2016-09-19 5 views
3

다음 코드를 사용하여 threading을 사용하고 현재 카운트를 인쇄합니다.파이썬 : 특정 횟수의 스레딩

import threading 

count = 0 
def worker(): 
    """thread worker function""" 
    global count 
    count += 1 
    print(count) 

threads = [] 
for i in range(5): 
    t = threading.Thread(target=worker) 
    threads.append(t) 
    t.start() 

현재 5 개의 스레드로 설정되어 있습니다. 스레드가 특정 #에 도달 할 때까지 스레드를 계속 실행하려면 어떻게합니까? worker()이 100 번 실행될 때까지 5 스레드에서 실행됩니다.

답변

2

while 루프를 반복하되 카운터와 테스트를 잠금으로 보호하십시오. 그렇지 않으면 테스트 된 값이 방금 증가시킨 값과 다릅니다.

스레드 ID를 추가 했으므로 어느 스레드가 실제로 카운터를 늘리는 지 확인할 수 있습니다.

또한 확인 : 이후에 증가합니다.

마지막에 스레드를 기다립니다.

import threading 

lck = threading.Lock() 

count = 0 
def worker(): 
    global count 
    """thread worker function""" 
    while True: 
     lck.acquire() 
     if count==100: 
      lck.release() 
      break 
     count += 1 
     print(threading.current_thread() ,count) 
     lck.release() 

threads = [] 
for i in range(5): 
    t = threading.Thread(target=worker) 
    threads.append(t) 
    t.start() 

for t in threads: 
    t.join() 

결과 :

(<Thread(Thread-1, started 5868)>, 1) 
(<Thread(Thread-2, started 7152)>, 2) 
(<Thread(Thread-3, started 6348)>, 3) 
(<Thread(Thread-4, started 6056)>, 4) 
(<Thread(Thread-1, started 5868)>, 5) 
(<Thread(Thread-5, started 5748)>, 6) 
(<Thread(Thread-2, started 7152)>, 7) 
(<Thread(Thread-3, started 6348)>, 8) 
(<Thread(Thread-4, started 6056)>, 9) 
(<Thread(Thread-1, started 5868)>, 10) 
(<Thread(Thread-5, started 5748)>, 11) 
(<Thread(Thread-2, started 7152)>, 12) 
(<Thread(Thread-3, started 6348)>, 13) 
(<Thread(Thread-4, started 6056)>, 14) 
(<Thread(Thread-1, started 5868)>, 15) 
(<Thread(Thread-5, started 5748)>, 16) 
(<Thread(Thread-2, started 7152)>, 17) 
(<Thread(Thread-3, started 6348)>, 18) 
(<Thread(Thread-4, started 6056)>, 19) 
(<Thread(Thread-1, started 5868)>, 20) 
(<Thread(Thread-5, started 5748)>, 21) 
(<Thread(Thread-2, started 7152)>, 22) 
(<Thread(Thread-3, started 6348)>, 23) 
(<Thread(Thread-4, started 6056)>, 24) 
(<Thread(Thread-1, started 5868)>, 25) 
(<Thread(Thread-5, started 5748)>, 26) 
(<Thread(Thread-2, started 7152)>, 27) 
(<Thread(Thread-3, started 6348)>, 28) 
(<Thread(Thread-4, started 6056)>, 29) 
(<Thread(Thread-1, started 5868)>, 30) 
(<Thread(Thread-5, started 5748)>, 31) 
(<Thread(Thread-2, started 7152)>, 32) 
(<Thread(Thread-3, started 6348)>, 33) 
(<Thread(Thread-4, started 6056)>, 34) 
(<Thread(Thread-1, started 5868)>, 35) 
(<Thread(Thread-5, started 5748)>, 36) 
(<Thread(Thread-2, started 7152)>, 37) 
(<Thread(Thread-3, started 6348)>, 38) 
(<Thread(Thread-4, started 6056)>, 39) 
(<Thread(Thread-1, started 5868)>, 40) 
(<Thread(Thread-5, started 5748)>, 41) 
(<Thread(Thread-2, started 7152)>, 42) 
(<Thread(Thread-3, started 6348)>, 43) 
(<Thread(Thread-4, started 6056)>, 44) 
(<Thread(Thread-1, started 5868)>, 45) 
(<Thread(Thread-5, started 5748)>, 46) 
(<Thread(Thread-2, started 7152)>, 47) 
(<Thread(Thread-3, started 6348)>, 48) 
(<Thread(Thread-4, started 6056)>, 49) 
(<Thread(Thread-1, started 5868)>, 50) 
(<Thread(Thread-5, started 5748)>, 51) 
(<Thread(Thread-2, started 7152)>, 52) 
(<Thread(Thread-3, started 6348)>, 53) 
(<Thread(Thread-4, started 6056)>, 54) 
(<Thread(Thread-1, started 5868)>, 55) 
(<Thread(Thread-5, started 5748)>, 56) 
(<Thread(Thread-2, started 7152)>, 57) 
(<Thread(Thread-3, started 6348)>, 58) 
(<Thread(Thread-4, started 6056)>, 59) 
(<Thread(Thread-1, started 5868)>, 60) 
(<Thread(Thread-5, started 5748)>, 61) 
(<Thread(Thread-2, started 7152)>, 62) 
(<Thread(Thread-3, started 6348)>, 63) 
(<Thread(Thread-4, started 6056)>, 64) 
(<Thread(Thread-1, started 5868)>, 65) 
(<Thread(Thread-5, started 5748)>, 66) 
(<Thread(Thread-2, started 7152)>, 67) 
(<Thread(Thread-3, started 6348)>, 68) 
(<Thread(Thread-4, started 6056)>, 69) 
(<Thread(Thread-1, started 5868)>, 70) 
(<Thread(Thread-5, started 5748)>, 71) 
(<Thread(Thread-2, started 7152)>, 72) 
(<Thread(Thread-3, started 6348)>, 73) 
(<Thread(Thread-4, started 6056)>, 74) 
(<Thread(Thread-1, started 5868)>, 75) 
(<Thread(Thread-5, started 5748)>, 76) 
(<Thread(Thread-2, started 7152)>, 77) 
(<Thread(Thread-3, started 6348)>, 78) 
(<Thread(Thread-4, started 6056)>, 79) 
(<Thread(Thread-1, started 5868)>, 80) 
(<Thread(Thread-5, started 5748)>, 81) 
(<Thread(Thread-2, started 7152)>, 82) 
(<Thread(Thread-3, started 6348)>, 83) 
(<Thread(Thread-4, started 6056)>, 84) 
(<Thread(Thread-1, started 5868)>, 85) 
(<Thread(Thread-5, started 5748)>, 86) 
(<Thread(Thread-2, started 7152)>, 87) 
(<Thread(Thread-3, started 6348)>, 88) 
(<Thread(Thread-4, started 6056)>, 89) 
(<Thread(Thread-1, started 5868)>, 90) 
(<Thread(Thread-5, started 5748)>, 91) 
(<Thread(Thread-2, started 7152)>, 92) 
(<Thread(Thread-3, started 6348)>, 93) 
(<Thread(Thread-4, started 6056)>, 94) 
(<Thread(Thread-1, started 5868)>, 95) 
(<Thread(Thread-5, started 5748)>, 96) 
(<Thread(Thread-2, started 7152)>, 97) 
(<Thread(Thread-3, started 6348)>, 98) 
(<Thread(Thread-4, started 6056)>, 99) 
(<Thread(Thread-1, started 5868)>, 100) 
2

루프 그것은, 물론.

lock = threading.Lock() 
count = 0 
def worker(): 
    """thread worker function""" 
    global count 
    while True: 
     with lock: 
      if count >= 100: break 
      count += 1 
      print(count) 

공지 사항 threading.Lockcount에 보호 액세스; GIL에 의존하는 것은 간단합니다.

1

솔직히 말해서 이와 같은 것을 원하면 잘못된 방향으로 가고 있음을 의미합니다. 이 코드는 stateful이고 stateful이므로 실제 병렬 실행과 멀리 떨어져 있습니다. 또한 보통 Python에서 코드를 병렬로 실행하려면 multiprocessing 모듈을 사용해야합니다.

당신의 목표는 총 100 번을 체크하는 경우 그래서, 기본적으로, 그것은 비 저장 방식으로 코드를 다시 작성하는 것이 좋습니다 :

import multiprocessing as mp 

def worker_1(x): 
    for i in range(x) 
     print i 

def worker_2(y): 
    print y 

if __name__ == '__main__': 
    p = mp.Pool(5) 

    for x in p.pool(worker_1, [25, 25, 25, 25]): 
    // process result 
    pass 

    for y in p.pool(worker_2, range(100)): 
    // process result 
    pass