迭代器和生成器3

扶醉桌前 提交于 2019-12-05 02:04:51

总结:生成器并行,实际上是串行的,但是时间上给人的感觉是并行的

import timedef product(name):    print("%s 准备" %name)    while True:        try:            name1 = yield                      #yield生产器中实现并行效果            # print("here")            print("[%s]开始了,[%s]准备" %(name1,name))        except StopIteration as e:            print(e.value)            print("here")c = product("ripple")c.__next__()b1 = "wangzi"c.send(b1)                                    #发送一个值作为yield的返回值c.__next__()def produces(name):    c =product('A')    c2 =product('B')    c.__next__()    c2.__next__()    print("比赛开始了")    for i in range(10):        time.sleep(0.5)        print("一共十场比赛")        c.send(i)        c2.send(i)produces("ripple")运行结果:

 

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!