why gevent.Timeout() can't raise Exception

心已入冬 提交于 2019-12-13 01:43:57

问题


with gevent.Timeout(0.1) as tt:

time.sleep(1)

above ,not raise Exception

with gevent.Timeout(0.1) as tt:

gevent.sleep(1)

throw gevent.timeout.Timeout: 0.1 seconds

there difference is time.sleep() and gevent.sleep()!


回答1:


time.sleep() actually pauses all execution of code and does not allow any other code to run. Gevent is an event-loop, which means that it allows other "threads" (greenlets) to run while blocking.

Essentially gevent has a list of tasks it is executing. It only allows 1 task to run at a time. If you say time.sleep(1), that task is still running, but not doing anything. If you say gevent.sleep(1), it pauses the current task and allows the other tasks to run.

gevent.Timeout() actually launches a second task to monitor the amount of time has elapsed. Since time.sleep() never yields, that second task never gets a chance to throw the error.



来源:https://stackoverflow.com/questions/32111239/why-gevent-timeout-cant-raise-exception

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