@生成器generator
a=(i*2 for i in range(10)) a.__next__()#等同于next(a),基本都不用,多用for循环
a.send(m)#将m传为yield的值
@生成器实例(IO异步的雏形)
import time def consumer(name): print("%s 准备吃包子啦!" %name) while True: baozi = yield print("包子[%s]来了,被[%s]吃了!" %(baozi,name)) def producer(name): c = consumer('A') c2 = consumer('B') c.__next__() c2.__next__() print("老子开始准备做包子啦!") for i in range(10): time.sleep(1) print("做了2个包子!") c.send(i) c2.send(i) producer("alex")
@