Twisted reactor.callLater() 未被尝试捕获,仅在某些机器上除外
Twisted reactor.callLater() not caught in try except only on some machines
我正在使用 Twisted python 并且代码基本上如下所示:
class TimeoutException(Exception):
pass
def timeout(msg):
raise TimeoutExpection(msg)
def create_timeout(len, msg):
return reactor.callLater(len, timeout, msg)
在代码的其他地方...
try:
timeout_call = create_timeout(10, "timeout")
db_res = yield some_large_io_request()
except Exception as e:
print e
raise web.HTTPError(408) # cyclone error
else:
if timeout_call.active():
timeout_call.cancel()
这是 运行 在 Docker 容器中,但是在某些机器上,try except 有效,在其他机器上,我收到类型 TimeoutException 的未处理错误,但我仍然收到 408 .我不清楚how/why它是断断续续的,有什么想法吗?
reactor.callLater
returns马上。它唯一做的就是安排一个函数在未来某个时间点被事件循环调用。
考虑:
from twisted.internet.task import react
from twisted.internet.defer import Deferred
def foo():
raise Exception("oh no")
def main(reactor):
try:
call = reactor.callLater(0, foo)
except:
print("Handling an exception!")
else:
print("Not handling an exception at all.")
print("Got {}".format(call))
print("Goodbye.")
return Deferred()
react(main, [])
这个程序总是产生这个输出:
Not handling an exception at all.
Got <DelayedCall 0x7fd082e28758 [-3.71932983398e-05s] called=0 cancelled=0 foo()>
Goodbye.
Unhandled Error
Traceback (most recent call last):
File "/tmp/junk.py", line 18, in <module>
react(main, [])
File "/usr/lib/python2.7/dist-packages/twisted/internet/task.py", line 936, in react
_reactor.run()
File "/usr/lib/python2.7/dist-packages/twisted/internet/base.py", line 1195, in run
self.mainLoop()
File "/usr/lib/python2.7/dist-packages/twisted/internet/base.py", line 1204, in mainLoop
self.runUntilCurrent()
--- <exception caught here> ---
File "/usr/lib/python2.7/dist-packages/twisted/internet/base.py", line 825, in runUntilCurrent
call.func(*call.args, **call.kw)
File "/tmp/junk.py", line 5, in foo
raise Exception("oh no")
exceptions.Exception: oh no
因此,您永远不能使用 try/except 来处理您使用 callLater
安排的函数引发的异常。
我正在使用 Twisted python 并且代码基本上如下所示:
class TimeoutException(Exception):
pass
def timeout(msg):
raise TimeoutExpection(msg)
def create_timeout(len, msg):
return reactor.callLater(len, timeout, msg)
在代码的其他地方...
try:
timeout_call = create_timeout(10, "timeout")
db_res = yield some_large_io_request()
except Exception as e:
print e
raise web.HTTPError(408) # cyclone error
else:
if timeout_call.active():
timeout_call.cancel()
这是 运行 在 Docker 容器中,但是在某些机器上,try except 有效,在其他机器上,我收到类型 TimeoutException 的未处理错误,但我仍然收到 408 .我不清楚how/why它是断断续续的,有什么想法吗?
reactor.callLater
returns马上。它唯一做的就是安排一个函数在未来某个时间点被事件循环调用。
考虑:
from twisted.internet.task import react
from twisted.internet.defer import Deferred
def foo():
raise Exception("oh no")
def main(reactor):
try:
call = reactor.callLater(0, foo)
except:
print("Handling an exception!")
else:
print("Not handling an exception at all.")
print("Got {}".format(call))
print("Goodbye.")
return Deferred()
react(main, [])
这个程序总是产生这个输出:
Not handling an exception at all.
Got <DelayedCall 0x7fd082e28758 [-3.71932983398e-05s] called=0 cancelled=0 foo()>
Goodbye.
Unhandled Error
Traceback (most recent call last):
File "/tmp/junk.py", line 18, in <module>
react(main, [])
File "/usr/lib/python2.7/dist-packages/twisted/internet/task.py", line 936, in react
_reactor.run()
File "/usr/lib/python2.7/dist-packages/twisted/internet/base.py", line 1195, in run
self.mainLoop()
File "/usr/lib/python2.7/dist-packages/twisted/internet/base.py", line 1204, in mainLoop
self.runUntilCurrent()
--- <exception caught here> ---
File "/usr/lib/python2.7/dist-packages/twisted/internet/base.py", line 825, in runUntilCurrent
call.func(*call.args, **call.kw)
File "/tmp/junk.py", line 5, in foo
raise Exception("oh no")
exceptions.Exception: oh no
因此,您永远不能使用 try/except 来处理您使用 callLater
安排的函数引发的异常。