import time
def eat(name):
while True:#因为是生成器,是一个有限循环所以这里要加while配合
baozi=yield#遇到yield退出生成器,并记录退出点
print('第%s轮包子,被%s吃了!' %(baozi,name))
def produce():
customer1=eat('a')#生成器对象赋值给变量
customer2=eat('b')
next(customer1)#next函数加变量进入生成器内部
next(customer2)
for i in range(10):#通过for循环决定了生成器的循环次数
time.sleep(1)
print('做了两个包子')
customer1.send(i)#函数.send根据记录点进入生成器,并将值传给yield前的变量
customer2.send(i)
produce()
python吃包子伪并发练习
最新推荐文章于 2024-01-11 09:34:34 发布