天天看点

Python编程:Coroutine协程之生产者消费者模型

# 消费者
def customer():
    a = 0
    while True:
        a = yield a
        print("a = %s"% a)

# 生产者
def producer(c):
    c.send(None) # 启动生成器
    for i in range(5):
        b = c.send(i)
        print("b = %s"% b)
    c.close()


producer(customer())

"""
a = 0
b = 0
a = 1
b = 1
a = 2
b = 2
a = 3
b = 3
a = 4
b = 4
"""