迭代器和生成器3

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

import time
def 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")
运行结果:

猜你喜欢

转载自www.cnblogs.com/mygodswangzi/p/11896796.html