import threading
from queue import Queue
from random import choice
dealList=["红烧猪蹄", "卤鸡爪", "酸菜鱼", "糖醋里脊", "九转大肠", "阳春面", "烤鸭", "烧鸡", "剁椒鱼头", "酸汤肥牛", "炖羊肉"]
queue=Queue(maxsize=5)
#厨子生产
def func_one(name):
for num in range(4):
data_veg=choice(dealList)
queue.put(data_veg,block=True)
print(f"厨师{name}给大家做了一道:{data_veg}")
#客人消费
def func_two(name:str):
for num in range(3):
veg_data=queue.get()
print(f"客人{name}吃掉了:{veg_data}")
queue.task_done()
if __name__=="__main__":
# 创建生产者线程,总共三个厨子,相当于给每个厨子创建了一个线程
for name in["张三","李四","王五"]:
thread_one=threading.Thread(target=func_one,args=(name,))
thread_one.start()
# 创建消费者四个线程
for name in ["客人甲", "客人乙", "坤哥", "凡哥"]:
thread_two=threading.Thread(target=func_two,args=(name,))
thread_two.start()
queue.join()
标签:__,消费者,生产者,模式,queue,veg,func,data,name
From: https://www.cnblogs.com/tudoot/p/18188111