队列与进程池
队列Queue (先进先出)
put
get 当没有值的时候会进入等待状态
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 from multiprocessing import Process,Queue 2 def func(q): 3 print('子进程{}'.format(q.get())) 4 q.put({"123":123}) 5 if __name__ == '__main__': 6 q=Queue() 7 q.put(123) 8 p=Process(target=func,args=(q,)) 9 p.start() 10 p.join() 11 print("主进程{}".format(q.get())) 12 13 子进程123 14 主进程{'123': 123}
put_nowait
get_nowait 没有值的时候会报错
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 from multiprocessing import Queue 2 a=Queue(3) 3 a.put_nowait(1) 4 a.put_nowait(2) 5 a.put_nowait(3) 6 # a.put_nowait(4) #报错queue.Full 7 print(a.get_nowait()) 8 print(a.get_nowait()) 9 print(a.get_nowait()) 10 print(a.get_nowait()) #报错queue.Empty
empty
full
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 from multiprocessing import Queue 2 a=Queue(3) 3 print(a.empty()) 4 print(a.full())
生产消费者模型
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 import time 2 import random 3 from multiprocessing import Process,Queue 4 def producer(q,name,cloth): 5 for i in range(1,20): 6 time.sleep(random.uniform(0.3,0.5)) 7 q.put('{}{}'.format(cloth,i)) 8 print('{}生产了{}{}'.format(name,cloth,i)) 9 def customer(q,name): 10 while 1: 11 a=q.get() 12 if a== None:break 13 time.sleep(random.uniform(0.2,0.4)) 14 print("{}买了{}".format(name,a)) 15 if __name__ == "__main__": 16 q=Queue() 17 p=Process(target=producer,args=(q,'tom',"T-shirt")) 18 p.start() 19 p1 = Process(target=customer, args=(q, 'mike')) 20 p1.start() 21 p.join() 22 q.put(None)
进程池
计算密集型 充分利用cup,多进程可以充分利用多核
适合开启多进程,但是不适合开启很多多进程
IO密集型 大部分时间都是在阻塞队伍,而不是在运行状态中
不适合开启多进程
同步提交 apply
返回值:子进程对应函数的返回值
一个一个顺序执行,并没有任何并发效果
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 from multiprocessing import Pool 2 def func(i): 3 print("book{}".format(i)) 4 if __name__ == '__main__': 5 p=Pool(4) 6 for i in range(1,11): 7 p.apply(func,args=(i,)) 8 同步 9 10 book1 11 book2 12 book3 13 book4 14 book5 15 book6 16 book7 17 book8 18 book9 19 book10
异步提交 apply_async
需要有close(),join(),
有返回值的时候用get()取值
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 from multiprocessing import Pool 2 import time 3 import random 4 def func(i): 5 time.sleep(random.random()) 6 print("book{}".format(i)) 7 if __name__ == '__main__': 8 p=Pool(4) 9 for i in range(1,11): 10 p.apply_async(func,args=(i,)) 11 p.close() 12 p.join()
map()方法 异步提交的简化,自带close和join
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
from multiprocessing import Pool import time import random def func(i): time.sleep(random.random()) print("book{}".format(i)) if __name__ == '__main__': p=Pool(4) p.map(func,range(1,11))
有返回值的时候,可以get()取值
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 from multiprocessing import Pool 2 def func(i): 3 return i 4 if __name__ == '__main__': 5 p=Pool() 6 lis=[] 7 for i in range(10): 8 ret=p.apply_async(func,args=(i,)) 9 lis.append(ret) 10 p.close() 11 p.join() 12 print(lis[0].get())