协程是一种轻量级的线程
无需线程上下级的开销, 所有的协程都在一个线程内执行
import time
def consumer(name):
print('%s is start to eat baozi'%name)
while True:
baozi = yield #用于传递
print('%s is start to eat %s baozi'%(name, baozi))
def Producter(name):
print('%s is start to make baozi'%name)
c1.__next__() #c1执行一次
c2.__next__() #c2 执行一次
for i in range(10):
time.sleep(2)
c1.send(i) #给生成器c1传递参数,生成器在yield处启动执行,在yield重新中断
c2.send(i)
if __name__ == '__main__':
c1 = consumer('wang') #生成c1 生成器
c2 = consumer('li') #生成c2 生成器
Producter('alex')