生产者-消费者经典单线程问题
import timedef 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")执行加一的生成器:
def gen(x):
count = x
while True:
val = (yield count)
if val is not None:
count = val
else:
count += 1
count = x
while True:
val = (yield count)
if val is not None:
count = val
else:
count += 1
f = gen(5)
print f.next()
print f.next()
print f.next()
print '===================='
print f.send(9)#发送数字9给生成器
print f.next()
print f.next()
浙公网安备 33010602011771号