python 利用yield实现一个异步请求

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('做了两个包子')
        c.send(i)
        c2.send(i)
        pass
#通过send 方法向yield传输值;yield通过send接受值;
#也就是生产者向消费者传递物品;
#串行中实现异步的过程;
producer('producer')

猜你喜欢

转载自www.cnblogs.com/bluesl/p/9079692.html
今日推荐