yield伪并发练习

def consumer(name):
    print("%s 开始购买物品了!" % name)
    while True:
        item_name = yield
        print("物品 %s 被 %s 购买了!" % (item_name, name))


def producer(name):
    c = consumer("a")
    c1 = consumer("b")
    next(c)
    next(c1)
    for i in range(10):
        print("%s 生产了2个物品 %s" % (name, i))
        c.send(i)
        c1.send(i)


producer("aaa")

 

posted @ 2024-09-13 16:26  GDquicksand  阅读(2)  评论(0编辑  收藏  举报