1,生产者与消费者模型的矛盾在于数据供需的不平衡
import time import random from multiprocessing import Queue from multiprocessing import Process def producer(q,food): for i in range(5): q.put(‘%s-%s‘%(food,i)) print(‘生产了%s‘%food) time.sleep(random.random()) q.put(None) q.put(None) q.put(None)#有三个消费者因此需要三个信号 def consumer(q,name): while True: food = q.get() if food == None:break print(‘%s 吃了 %s‘%(name,food)) if __name__ == ‘__main__‘: q = Queue() p1 = Process(target=producer,args=(q,‘菠萝蜜干‘)) p1.start() p2 = Process(target=producer, args=(q, ‘酸奶‘)) p2.start() c1 = Process(target=consumer, args=(q, ‘Rabbit‘)) c1.start() c2 = Process(target=consumer, args=(q, ‘OrangeCat‘)) c2.start() c3 = Process(target=consumer, args=(q, ‘CuiHua‘)) c3.start()
生产者消费者模型
1.消费者要处理多少数据是不确定的
2.所以只能用while循环来处理数据 ,但是while循环无法结束
3.需要生产者发送信号
4.有多少个消费者 就需要发送多少个信号
5.但是发送的信号数量需要根据 生产者和消费者的数量进行计算,所以非常不方便
2,JoinableQueue
import time import random from multiprocessing import Process from multiprocessing import JoinableQueue def producer(q,food): for i in range(5): q.put(‘%s-%s‘%(food,i)) print(‘生产了%s‘%food) time.sleep(random.random()) q.join() # 等待消费者把所有的数据都处理完 def consumer(q,name): while True: food = q.get() # 生产者不生产还是生产的慢 print(‘%s 吃了 %s‘%(name,food)) q.task_done()#JoinableQueue内部自带计数功能,每执行一次task_done,计数减一。
if __name__ == ‘__main__‘:
q = JoinableQueue()
p1 = Process(target=producer,args=(q,‘炒河粉‘))
p1.start()
p2 = Process(target=producer, args=(q, ‘草莓‘))
p2.start()
c1 = Process(target=consumer, args=(q, ‘Rabbit‘))
c1.daemon = True
c1.start()
c2 = Process(target=consumer, args=(q, ‘Orange_Cat‘))
c2.daemon = True
c2.start()
c3 = Process(target=consumer, args=(q, ‘Teddy‘))
c3.daemon = True
c3.start()
p1.join() # 等待p1执行完毕
p2.join() # 等待p2执行完毕
生产者生产的数据全部被消费 —— 生产者进程结束 —— 主进程代码执行结束 —— 消费者守护进程结束