import random
import time
cacheList = []
cacheListLen = 5
def isfull():
return len(cacheList) == 5
def consumer(name):
"""消费者"""
print("%s准备买包子......." %(name))
while True:
kind = yield
print("%s购买%s包子成功..." %(name, kind))
def producer(name):
print("%s厨师正在生产包子......." %(name))
kinds = ['A', 'B', 'C', 'D']
while True:
if not isfull():
time.sleep(random.random())
kind = random.choice(kinds)
print("%s已经生产好%s包子了..." %(name, kind))
cacheList.append(kind)
else:
print("已经有足够的包子")
yield
p = producer("hello")
next(p)
consumers = [consumer('user'+str(i)) for i in range(10)]
for i in consumers:
if not cacheList:
print("目前没有包子")
else:
if not isfull():
next(p)
kind = cacheList.pop()
next(i)
i.send(kind)
