为什么gevent.Timeout()无法引发异常 [英] why gevent.Timeout() can't raise Exception

查看:244
本文介绍了为什么gevent.Timeout()无法引发异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将gevent.Timeout(0.1)设置为tt:

with gevent.Timeout(0.1) as tt:

time.sleep(1)

高于,不引发异常

将gevent.Timeout(0.1)设置为tt:

with gevent.Timeout(0.1) as tt:

gevent.sleep(1)

抛出gevent.timeout.Timeout:0.1秒

throw gevent.timeout.Timeout: 0.1 seconds

time.sleep()和gevent.sleep()有所不同!

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

推荐答案

time.sleep()实际上暂停所有代码执行,并且不允许任何其他代码运行. Gevent是一个事件循环,这意味着它允许其他线程"(greenlet)在阻塞时运行.

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.

基本上,gevent包含正在执行的任务的列表.一次只允许执行一项任务.如果您说time.sleep(1),则该任务仍在运行,但未执行任何操作.如果您说gevent.sleep(1),它将暂停当前任务并允许其他任务运行.

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()实际上启动了第二个任务,以监视已过去的时间.由于time.sleep()永不屈服,因此第二个任务永远也没有机会抛出错误.

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.

这篇关于为什么gevent.Timeout()无法引发异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆