天天看點

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
"""