Python生成器实现生产者消费者模式

2019-04-09  本文已影响0人  AI拆书人汤问
import time
def consumer():
    t = ''
    while True:
        result = yield t
        print("消费{}".format(result))
        time.sleep(5)
        print("休息5秒....")

def product():
    c1 = consumer()
    c2 = consumer()
    c1.send(None)
    c2.send(None)
    for i in range(10):
        print("生产{}".format(i))
        c1.send(i)
        c2.send(i)
    c1.close()
    c2.close()

product()
上一篇 下一篇

猜你喜欢

热点阅读