简单的携程程序

import time
def consumer(name):
    print("%s准备吃包子了"%name)
    while True:
        baozi = yield
        print("[%s]包子来了,[%s]吃吧"% (baozi,name))
# c = consumer("Caicai")
# c.__next__()###只能调用,不能传值
# c.send("韭菜")###通过send()向生成器中的yield传一个值进去
def produ(name):
    proper_1 = consumer("Caicai")
    proper_2 = consumer("CaiCai")
    proper_1.__next__()
    proper_2.__next__()
    print("准备好了吗")
    for i in range(10):
        time.sleep(2)
        print("%s:两个包子好了"%name)
        proper_1.send(i)
        proper_2.send(i)
produ("DaDa")

 

posted @ 2018-04-25 12:58  Caionk  阅读(286)  评论(0编辑  收藏  举报