def g_test(): while True: n = yield # 收到的值 给n print("receive from outside:",n) g = g_test() g.__next__() # 调用生成器, 同时会发送None 到 yield for i in range(10): g.send(i) # 调用生成器, 同时发送i
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)
原文地址:https://www.cnblogs.com/kissfire008/p/11751301.html
时间: 2024-11-05 15:53:30