-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathch20_7.py
More file actions
46 lines (33 loc) · 1.39 KB
/
ch20_7.py
File metadata and controls
46 lines (33 loc) · 1.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# 队列就是时序控制器,让程序按照队列的要求进行。
# 若把队列放在for循环中,队列等待时,for循环会一起等待,起暂停作用。
# 如第39行:队列在等下一组数据时,for循环就会暂停等待
# Queue队列的get()方法:在队列(容器)为空时会一直等待,暂停,直到有数据进入。
# 标准库queue模块提供多种队列:Queue | LifoQueue | PriorityQueue
import time
import threading
import queue
work_queue = queue.Queue(maxsize=10)
result_queue = queue.Queue(maxsize=10)
class WorkerThread(threading.Thread):
def __init__(self, thread_id):
super(WorkerThread, self).__init__()
self.thread_id = thread_id
def run(self):
while not work_queue.empty():
work = work_queue.get()
time.sleep(3)
out = 'Thread %d\t received %s' % (self.thread_id, work)
result_queue.put(out)
def main():
print('Main thread start')
for item in range(10):
work_queue.put('message id %d' % item)
for item in range(2):
thread = WorkerThread(item)
thread.start()
for item in range(10): # 本质上是自动调用threading.lock
result = result_queue.get() # 多个线程可以共用同一个Queue实例,阻塞for循环
print(result)
print('Main thread finish')
if __name__ == '__main__':
main()