标签:阻塞 join 尺度 数据传递 聊天 art 不同 阻塞队列 子进程
1 import time 2 from multiprocessing import Process 3 4 5 # 多进程执行多任务 6 # 多进程不共享全局变量 7 def work1(): 8 for i in range(5): 9 print("这个是任务1------") 10 time.sleep(0.5) 11 12 13 def work2(): 14 for i in range(5): 15 print("这个是任务2------") 16 time.sleep(0.5) 17 18 19 if __name__ == ‘__main__‘: 20 # windows系统py创建进程不加__main__会进入无限递归状态 21 p1 = Process(target=work1) 22 p2 = Process(target=work2) 23 24 p1.start() 25 p2.start()
1 # 多进程间相互通信 2 from multiprocessing import Process, Queue 3 count = 1 4 def work1(q): 5 while q.qsize() > 0: 6 global count 7 q.get() 8 print("这个是任务1------") 9 print("执行%s 次" % count) 10 count += 1 11 12 def work2(q): 13 while q.qsize() > 0: 14 global count 15 q.get() 16 print("这个是任务2------") 17 print("执行%s 次" % count) 18 count += 1 19 20 if __name__ == ‘__main__‘: 21 q = Queue() 22 for i in range(10): 23 q.put("hello") 24 p1 = Process(target=work1, args=(q,)) 25 p2 = Process(target=work2, args=(q,)) 26 27 p1.start() 28 p2.start()
标签:阻塞 join 尺度 数据传递 聊天 art 不同 阻塞队列 子进程
原文地址:https://www.cnblogs.com/xi77/p/14529274.html