python 进程间通信 Queue教程
1 """
2 进程间通信
3 """
4 import time
5 from multiprocessing import Pool, Queue, Process
6
7
8 def pull_task(q: Queue):
9 tasks = ["任务1", "任务2", "任务3", "任务4", "任务5", "任务6", "任务7"]
10 for task in tasks:
11 print("正在执行", task)
12 time.sleep(1)
13 q.put(task)
14
15
16 def get_task(q: Queue):
17 while True:
18 try:
19 print(q.get(timeout=5))
20 except:
21 print("任务全部完成")
22 break
23
24
25 if __name__ == '__main__':
26 q = Queue(5)
27 p1 = Process(target=pull_task, args=(q,))
28 p2 = Process(target=get_task, args=(q,))
29 p1.start()
30 p2.start()
31 p1.join()
32 p2.join()
33
34 print("over")
发表评论