知识点:
send(i) :唤醒yield,并将 i 的值传给 yield
#!/user/bin env python# author:Simple-Sir# time:20181020# 单线程并发:做一个包子,两个人吃import time# 吃包子生成器def chibaozi(name): print('%s想吃包子了!' %name) while True: baozi = yield print('第%s个包子来了,被%s吃了一半!'%(baozi,name))# 逻辑解析# c = chibaozi('SimpleGirl')# c.__next__() # 唤醒yield# b1='牛肉包子'# c.send(b1) # 注:唤醒yield,并将b1的值传给 yield# 做包子生成器def zuobaozi(name): c1 = chibaozi('SimpleSir') c2 = chibaozi('SimpleGirl') c1.__next__() c2.__next__() print('%s开始做包子了!'%name) for i in range(1,6): time.sleep(1) print('%s做了1个包子!'%name) c1.send(i) c2.send(i)# 调用函数zuobaozi('SimpleSir')
执行结果: