问题
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