生成器实例

#生成器,只有在调用时,才会生产相应的数据,不能通过切片取
#c.__next__()取下一个。只记住当前的位置,只有一个__next__()方法。
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("做了1个包子,分两半!")
c.send(i+1)
c2.send(i+1)

producer("alex")

猜你喜欢

转载自www.cnblogs.com/wzsx/p/9121132.html