Skip to content

Commit dbd08f5

Browse files
committed
queue threading
1 parent 66cd79d commit dbd08f5

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import queue
2+
import threading
3+
4+
q = queue.Queue()
5+
6+
def worker(num):
7+
while True:
8+
item = q.get()
9+
if item is None:
10+
break
11+
# 작업을 처리한다.
12+
print(f"스레드 {num+1} : 처리 완료 {item}")
13+
q.task_done()
14+
15+
if __name__ == '__main__':
16+
num_worker_threads = 5
17+
threads = []
18+
for i in range(num_worker_threads):
19+
t = threading.Thread(target=worker, args=(i,))
20+
t.start()
21+
threads.append(t)
22+
23+
for item in range(20):
24+
q.put(item)
25+
26+
# 모든 작업이 끝날때까지 대기한다(block).
27+
q.join()
28+
29+
# 워커 스레드를 종료한다(stop).
30+
for i in range(num_worker_threads):
31+
q.put(None)
32+
for t in threads:
33+
t.join()

0 commit comments

Comments
 (0)