python生成器迭代器

来源:互联网 发布:selfiecity软件下载 编辑:程序博客网 时间:2024/05/21 03:18
#单线程实现并发import timedef consumer(name):    print("%s 准备吃包子啦"%name)    while True:        baozi=yield        print("包子[%s]来了,被[%s]吃了"%(baozi,name))# c=consumer("张")# c.__next__()# c.__next__()# c.__next__()# b1="韭菜馅"# c.send(b1)#调用yield,同时传个值# c.__next__()def producer(name):    c=consumer('张')    c2=consumer('牛')    c.__next__()#准备吃包子    c2.__next__()    print("[%s]开始准备做包子啦"%name)    for i in range(10):        time.sleep(0.5)        print("做了一个包子,分两半")        c.send(i)        c2.send(i)producer("老妈")
原创粉丝点击