利用生成器 的 yield 实现伪并发
照着别人的写的,不成熟,请见谅.
import time
def consumer(name): # consumer 顾客
print("%s 准备吃包子了" %name)
while True:
baozi = yield
print("包子[%s]来了,被[%s]吃掉了" %(baozi,name))
def producer(name): # producer 制作人
c = consumer("A") #两个生成器对象
c2 = consumer("B") #两个生成器对象
next(c) # 这一步就是到consumer()里面while True的条件
next(c2)
print("开始做包子")
for i in range(1,10):
time.sleep(2)
print("做了2个包子")
c.send(i) # 到baozi=yield 把1赋给yield ,A吃掉了
c2.send(i+1)
producer("wu")