python 协程中的手动切换Greenlet和自动Gevent
作者:互联网
手动切换Greenlet
from greenlet import greenlet def test1(): print(12) gr2.switch()#第一次切换 print(34) gr2.switch()#第三次切换 def test2(): print(56) gr1.switch()#第二次切换 print(78) gr1 = greenlet(test1) gr2 = greenlet(test2) gr1.switch()greenlet
自动切换gevent
import gevent def func1(): print('\033[31;1m李闯在跟海涛搞...\033[0m') gevent.sleep(2)#遇上IO自动切换到 print('\033[31;1m李闯又回去跟继续跟海涛搞...\033[0m') def func2(): print('\033[32;1m李闯切换到了跟海龙搞...\033[0m') gevent.sleep(0.5)#遇上IO自动切换到 print('\033[32;1m李闯搞完了海涛,回来继续跟海龙搞...\033[0m') def func3(): print('running func3') gevent.sleep(0)#遇上IO自动切换到 print('runing func3 again') gevent.joinall([ gevent.spawn(func1),#生成, gevent.spawn(func2), gevent.spawn(func3), ])gevent
标签:协程,python,gevent,greenlet,切换,Gevent,print,033,def 来源: https://www.cnblogs.com/anhao-world/p/13744360.html