Eventlet超时未退出 [英] Eventlet timeout not exiting
问题描述
为什么eventlet允许它完成6秒,而在5秒后应该退出缩进?
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提供了协作式多线程.这意味着您需要放弃控制,以使集线器或协程(在这种情况下,集线器实现超时)有运行的机会.要进行产量控制:
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:
- 使用绿色版本的IO和睡眠
- 或执行
eventlet.monkey_patch()
,现在您可以使用常规的time
,socket
等模块,并与Eventlet合作用绿色"版本代替.
- either use green versions of IO and sleep
- or execute
eventlet.monkey_patch()
, now you can use regulartime
,socket
, etc modules, replaced by "green" versions, cooperating with Eventlet.
任何没有绿色调用的CPU严格代码,例如[_ for _ in xrange(1000000000)]
都根本无法中断.如果您发现自己处于类似情况,请将eventlet.sleep(0)
放在循环中的某个位置,它将进入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.
这篇关于Eventlet超时未退出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!