Python 再次改進版通過隊列實現一個生產者消費者模型
阿新 • • 發佈:2019-01-14
code bsp 隊列 clas 任務 get time multi put
import time from multiprocessing import Process,Queue #生產者 def producer(q): for i in range(10): time.sleep(0.2) s = ‘大包子%s號‘%i print(s+‘新鮮出爐,拿去用‘) q.put(s) q.put(None) #發送一個任務結束信號,來中斷消費者的程序 def consumer(q): while 1: time.sleep(0.5) baozi = q.get()if baozi == None: print(‘都吃完了大哥,該回家伺候嫂子了‘) break print(baozi+‘被吃了‘) if __name__ == ‘__main__‘: q = Queue(30) pro_p = Process(target=producer,args=(q,)) con_p = Process(target=consumer,args=(q,)) pro_p.start() con_p.start()
Python 再次改進版通過隊列實現一個生產者消費者模型