python-生產者消費者模型
阿新 • • 發佈:2018-06-14
ons rand 生產者消費者模型 python target join Oz 問題 join()
生產者消費者是通過一個容器來解決生產者和消費者的強耦合問題。生產者和消費者不直接通訊,而通過阻塞隊列進行通信。阻塞隊列就相當一個緩沖區,平衡了生產者和消費者的處理能力。
import time,random import queue,threading q = queue.Queue() def Producer(name): count = 0 while count <10: print("making........") time.sleep(5) q.put(count) print(‘Producer %s has produced %s baozi..‘ %(name, count)) count +=1 #q.task_done() q.join() print("ok......") def Consumer(name): count = 0 while count <10: time.sleep(random.randrange(4)) # if not q.empty(): # print("waiting.....") #q.join() data = q.get() print("eating....") time.sleep(4) q.task_done() #print(data) print(‘\033[32;1mConsumer %s has eat %s baozi...\033[0m‘ %(name, data)) # else: # print("-----no baozi anymore----") count +=1 p1 = threading.Thread(target=Producer, args=(‘A君‘,)) c1 = threading.Thread(target=Consumer, args=(‘B君‘,)) c2 = threading.Thread(target=Consumer, args=(‘C君‘,)) c3 = threading.Thread(target=Consumer, args=(‘D君‘,)) p1.start() c1.start() c2.start() c3.start()
python-生產者消費者模型