e.g.
5个厨师做包子,三秒一个,10个食客吃包子,1秒一个,食客吃的又快人又多
import queue
import threading
import time
q = queue.Queue()
def cook(i):
while True:
q.put(f'厨师{i}做de包子')
time.sleep(2)
def hangers_on(j):
while True:
print(f'食客{j}吃了{q.get()}')
time.sleep(1)
for i in range(5):
threading.Thread(target=cook,args=(i,)).start()
for j in range(10):
threading.Thread(target=hangers_on,args=(j,)).start()
Original: https://www.cnblogs.com/jianhui-zhou/p/16379993.html
Author: 剑辉_周
Title: 设计模式-生产者消费者模式
原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/499369/
转载文章受原作者版权保护。转载请注明原作者出处!