python 之生产者消费者模型
进程实现:
import time,random from multiprocessing import Process,Queue def producer(name,q): count= 0 while count<10: print('making,,,,') time.sleep(2) q.put(count) print('Producer %s has produced %s baozi'%(name,count)) count += 1 def consumer(name,q): count = 0 while count <10: time.sleep(1) if not q.empty(): data = q.get() print(data) print('consumer %s has eat %s baozi'%(name,count)) count +=1 else: print('no baozi') if __name__ == '__main__': # 容器 q = Queue() # 生产者们 p = Process(target=producer,args=('A',q,)) p.start() # 消费者们 c = Process(target=consumer,args=('B',q,)) c.start()