python 基础(十六)生成器用法举例
2021-05-13 03:30
标签:int port ons 举例 col sleep import python 基础 def 协程 python 基础(十六)生成器用法举例 标签:int port ons 举例 col sleep import python 基础 def 原文地址:https://www.cnblogs.com/zbvc/p/13132259.htmlimport time
def consumer(name):
print(‘%s,准备吃包子‘%name)
while True:
baozi = yield
print(‘包子%s来了,被%s吃了‘%(baozi,name))
c = consumer(‘clyde‘)
c.__next__() #__next__():调用一次
b = ‘香菇油菜馅‘
c.send(b) #send():把参数发送过去在调用一次
import time
def consumer(name):
print(‘%s,准备吃包子‘%name)
while True:
baozi = yield
print(‘包子%s来了,被%s吃了‘%(baozi,name))
c = consumer(‘clyde‘)
c.__next__()
def producer(name):
c = consumer(‘张三‘)
c2 = consumer(‘的歌‘)
c.__next__()
c2.__next__()
print(‘我开始准备做包子了‘)
for i in range(10):
time.sleep(1)
print(‘做了一个包子‘)
c.send(i)
c2.send(i)
producer(‘name‘)
上一篇:java之lambda
文章标题:python 基础(十六)生成器用法举例
文章链接:http://soscw.com/index.php/essay/84963.html