'''
协程: 微线程
进程》线程》协程
'''
from time import sleep
def eat():
for i in range(5):
print('正在吃第{}个馒头'.format(i + 1))
sleep(0.6)
yield
def listen():
for i in range(5):
print('正在听第{}首歌'.format(i + 1))
sleep(0.6)
yield
if __name__ == '__main__':
g1 = eat()
g2 = listen()
while True:
try:
next(g1)
next(g2)
except:
break
'''
协程:
gevent----> greenlet
猴子补丁
'''
import time
import gevent
from gevent import monkey
# 猴子补丁
monkey.patch_all()
def eat():
for i in range(5):
print('正在吃第{}个馒头'.format(i + 1))
time.sleep(0.6) # 自动切换
def listen():
for i in range(5):
print('正在听第{}首歌'.format(i + 1))
time.sleep(0.6)
if __name__ == '__main__':
g1 = gevent.spawn(eat)
g2 = gevent.spawn(listen)
g1.join()
g2.join()
print('-----over-----')
版权声明:本文为CSDN博主「piduocheng0577」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/piduocheng0577/article/details/105107543
原文链接:https://blog.csdn.net/piduocheng0577/article/details/105107543