gevent携程,自动切换(IO)


import gevent

def foo():
print('Running in foo')
gevent.sleep(2)
print('Explicit context switch to foo again')
def bar():
print('Explicit精确的 context内容 to bar')
gevent.sleep(1)
print('Implicit context switch back to bar')
def func3():
print("running func3 ")
gevent.sleep(0)
print("running func3 again ")


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


#=======================

Running in foo
Explicit精确的 context内容 to bar
running func3
running func3 again
Implicit context switch back to bar
Explicit context switch to foo again

猜你喜欢

转载自www.cnblogs.com/rongye/p/9983366.html