def put_on(name): print("Hi {}, 货物来了,准备搬到仓库!".format(name)) while True: goods = yield print("货物[%s]已经被%s搬进仓库了。"%(goods,name)) p = put_on("bigberg") #输出 G:\python\install\python.exe G:/python/untitled/study4/test/double.py Process finished with exit code 0
print(type(p)) #输出 <class 'generator'>
def put_on(name): print("Hi {}, 货物来了,准备搬到仓库!".format(name)) while True: goods = yield #遇到yield中断 print("货物[%s]已经被%s搬进仓库了。"%(goods,name)) #中断后运行部分 p = put_on("bigberg") p.__next__() #输出 Hi bigberg, 货物来了,准备搬到仓库!
def put_on(name): print("Hi {}, 货物来了,准备搬到仓库!".format(name)) while True: goods = yield print("货物[%s]已经被%s搬进仓库了。"%(goods,name)) p = put_on("bigberg") p.__next__() p.__next__() #输出 Hi bigberg, 货物来了,准备搬到仓库! 货物[None]已经被bigberg搬进仓库了。
def put_on(name): print("Hi {}, 货物来了,准备搬到仓库!".format(name)) while True: goods = yield print("货物[%s]已经被%s搬进仓库了。"%(goods,name)) p = put_on("bigberg") p.__next__() p.send("瓜子") #输出 Hi bigberg, 货物来了,准备搬到仓库! 货物[瓜子]已经被bigberg搬进仓库了。
def put_on(name): print("Hi {}, 货物来了,准备搬到仓库!".format(name)) while True: goods = yield print("货物[%s]已经被%s搬进仓库了。"%(goods,name)) p = put_on("bigberg") p.__next__() p.send("瓜子") p.send("花生") p.send("饼干") p.send("牛奶") #多次调用send() Hi bigberg, 货物来了,准备搬到仓库! 货物[瓜子]已经被bigberg搬进仓库了。 货物[花生]已经被bigberg搬进仓库了。 货物[饼干]已经被bigberg搬进仓库了。 货物[牛奶]已经被bigberg搬进仓库了。
import time def put_on(name): print("Hi {}, 货物来了,准备搬到仓库!".format(name)) while True: goods = yield print("货物[%s]已经被%s搬进仓库了。"%(goods,name)) def transfer(name): p = put_on('A') p2 = put_on('B') p.__next__() p2.__next__() print("%s将货物送来了!"%name) for i in range(5): time.sleep(1) print("%s递过来两件货物"%name) p.send("瓜子") p2.send("花生") transfer("bigberg") #输出 Hi A, 货物来了,准备搬到仓库! Hi B, 货物来了,准备搬到仓库! bigberg将货物送来了! bigberg递过来两件货物 货物[瓜子]已经被A搬进仓库了。 货物[花生]已经被B搬进仓库了。 bigberg递过来两件货物 货物[瓜子]已经被A搬进仓库了。 货物[花生]已经被B搬进仓库了。 bigberg递过来两件货物 货物[瓜子]已经被A搬进仓库了。 货物[花生]已经被B搬进仓库了。 bigberg递过来两件货物 货物[瓜子]已经被A搬进仓库了。 货物[花生]已经被B搬进仓库了。 bigberg递过来两件货物 货物[瓜子]已经被A搬进仓库了。 货物[花生]已经被B搬进仓库了。
以上就是实例详解python生成器协程运算的详细内容,更多请关注Gxl网其它相关文章!
查看更多关于实例详解python生成器协程运算的详细内容...
声明:本文来自网络,不代表【好得很程序员自学网】立场,转载请注明出处:http://www.haodehen.cn/did84509