python eventlet超时_Eventlet超时未退出

Why does eventlet allow it to finish the 6 seconds, when it should exit the indentation after 5 seconds?

>>> with eventlet.Timeout(5):

time.sleep(6)

x = 1

>>> x

1

解决方案

Eventlet provides cooperative multithreading. Which means you need to yield control to give hub or coroutines (in this case, hub implements timeouts) chance to run. To yield control:

either use green versions of IO and sleep

or execute eventlet.monkey_patch(), now you can use regular time, socket, etc modules, replaced by "green" versions, cooperating with Eventlet.

Any CPU tight code without green calls, for example [_ for _ in xrange(1000000000)] is impossible to interrupt at all. If you find yourself in similar situation, place eventlet.sleep(0) somewhere in loop, that would enter Eventlet hub and allow timeouts to work.

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值