yield偽併發練習

GDquicksand發表於2024-09-13
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")

相關文章