【协程】由两个带 while循环的函数和yield组建的协程

def consumer():
r = ''
while True:
n = yield r
if not n:
return
print('[CONSUMER] Consuming %s...' % n)
r = '200 OK'


def produce(c):
c.send(None)
n = 0
while n < 5:
n = n + 1
print('[PRODUCE] Producing %s...' % n)
r = c.send(n)
print('[PRODUCE] Consumer return:%s' % r)
c.close()


c = consumer()
produce(c)
posted @ 2018-07-02 22:15  地瓜来了  阅读(450)  评论(0编辑  收藏  举报