#单线程实现并发 import time def consumer(name): print("%s 准备吃包子啦"%name) while True: baozi=yield print("包子[%s]来了,被[%s]吃了"%(baozi,name)) # c=consumer("张") # c.__next__() # c.__next__() # c.__next__() # b1="韭菜馅" # c.send(b1)#调用yield,同时传个值 # c.__next__() def producer(name): c=consumer('张') c2=consumer('牛') c.__next__()#准备吃包子 c2.__next__() print("[%s]开始准备做包子啦"%name) for i in range(10): time.sleep(0.5) print("做了一个包子,分两半") c.send(i) c2.send(i) producer("老妈")
python生成器迭代器
最新推荐文章于 2024-09-02 23:14:47 发布