立即学习:https://edu.csdn.net/course/play/24459/296353?utm_source=blogtoedu
向生成器中传值: def g_test(): while True: n = yield #将函数外面接收到的值赋给n print(n) g = g_test() g.__next__() #__next__调用生成器同时发送none for i in range(10): g.send(i)# 调用生成器同时发送i
单线程下的多并发效果
吃包子:c1,c2,c3
生产者:做baozi
def consumer(name): print("消费者%s准备吃包子了"%name) while True: baozi = yield #接收外面的包子 print("消费者%s收到包子编号%s"%(name,baozi)) c1 = consumer("c1") c2 = consumer("c2") c3 = consumer("c3") c1.__next__() c2.__next__() c3.__next__() for i in range(10): print("生产了第%s批包子"%i) c1.send(i) c2.send(i) c3.send(i)