We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 66cd79d commit dbd08f5Copy full SHA for dbd08f5
6장_파이썬_고급/1_threading_with_queue.py
@@ -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
31
+ q.put(None)
32
+ for t in threads:
33
+ t.join()
0 commit comments