gevent携程,自动切换(IO)

本文介绍了一个使用Python的gevent库进行并发操作的例子。通过定义和运行三个不同的协程任务,展示了如何实现任务间的显式和隐式上下文切换,以及gevent.sleep的使用方法。

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

转载于:https://www.cnblogs.com/rongye/p/9983366.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值