一个关于吃包子的例子(视频多看几遍):
import time
def consumer(name):
print('我是[%s],我要开始吃包子了' %name)
while True:
baozi = yield
time.sleep(1)
print('%s 很开心地把[%s]吃掉了' %(name,baozi))
def producer():
c1 = consumer('wupeiqi')
c2 = consumer('yuanhao')
c1.__next__()
c2.__next__()
for i in range(10):
time.sleep(1)
c1.send('包子 %s' %i)
c2.send('包子 %s' %i)
producer()