def consummer(name):
while True:
baozi = yield
print(‘包子%s来啦,被%s吃了‘%(baozi,name))
return None
c = consummer(‘紫星大人‘)
bz = ‘韭菜馅‘
c.send(bz) #唤醒yield并传值
c.__next__() #只唤醒不传值
模拟单线程下的并行效果#实际上应该是携程效果,比进程更小的一个单位
import time
def consummer(name):
print(‘顾客%s已经做好吃包子的准备了‘%name)
while True:
baozi = yield
print(‘包子%s来啦,被%s吃了‘%(baozi,name))
def producer():
c = consummer(‘张三‘)
c2 = consummer(‘李四‘)
c.__next__()
c2.__next__()
print(‘厨师开始做包子啦!‘)
for i in range(20,):
time.sleep(1)
print(‘生产出来2个包子‘)
c.send(i)
c2.send(i)
producer()
原文地址:http://daring.blog.51cto.com/891785/1887951