Eventlet timeout not exiting

拥有回忆 提交于 2019-12-11 10:41:50

问题


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

回答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.



来源:https://stackoverflow.com/questions/30795353/eventlet-timeout-not-exiting

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!