greenlet手动切换协程
from greenlet import greenlet

def tes_1():
   
print(12)
    gr2.switch()
#协程手动切换
   
print(34)
    gr2.switch()
#协程切换

def tes_2():
   
print(56)
    gr1.switch()
#协程切换
   
print(78)

gr1 = greenlet(tes_1) 
#生成两个协程
gr2 = greenlet(tes_2)
gr1.switch()
#协程切换

 

 

gevent自动切换协程
import gevent

def foo():
   
print("running in foo")
    gevent.sleep(
2) #IO阻塞2 自动切换下一个协程
   
print('Explict context switch to foo again')
def bar():
   
print("Explicit精确的 context上下文切换 to bar")
    gevent.sleep(
1)#IO阻塞1
   
print('Implicit context switch back to bar')
def func3():
   
print("running func3")
    gevent.sleep(
0)#IO不阻塞,释放 就先执行下面代码
   
print("running func3 again")

gevent.joinall([
    gevent.spawn(foo),
#生成协程
   
gevent.spawn(bar),
    gevent.spawn(func3),
])