python基础-协程

 

一个简单的协程案例:

# 时间,健康(身体健康,精神愉快),玛瑙


def eater(name):
    print("name:%s,start to eat"%name)
    food_list=[]
    while True:
        food=yield food_list
        print("%s eat %s"%(name,food))
        food_list.append(food)

    print("done")


e=eater("alex")
print(next(e))
print(e.send("包子"))
print(e.send("鲍鱼仔"))
print(e.send("蚝生仔"))

#
# def hello(name):
#     print("name:%s"%name)
#     yield hello
#
#
# h=hello("alex")
# next(h)

  

 

posted @ 2017-09-08 20:02  Adamanter  阅读(89)  评论(0编辑  收藏  举报