我正试图以这种方式处理AsyncClient.fetch
中发生的异常:
from tornado.httpclient import AsyncHTTPClient
from tornado.httpclient import HTTPRequest
from tornado.stack_context import ExceptionStackContext
from tornado import ioloop
def handle_exc(*args):
print('Exception occured')
return True
def handle_request(response):
print('Handle request')
http_client = AsyncHTTPClient()
with ExceptionStackContext(handle_exc):
http_client.fetch('http://some123site.com', handle_request)
ioloop.IOLoop.instance().start()
并查看下一个输出:
WARNING:root:uncaught exception
Traceback (most recent call last):
File "/home/crchemist/python-3.2/lib/python3.2/site-packages/tornado-2.0-py3.2.egg/tornado/simple_httpclient.py", line 259, in cleanup
yield
File "/home/crchemist/python-3.2/lib/python3.2/site-packages/tornado-2.0-py3.2.egg/tornado/simple_httpclient.py", line 162, in __init__
0, 0)
socket.gaierror: [Errno -5] No address associated with hostname
Handle request
我做错什么了?
我一点也不知道龙卷风,但我看了一眼,你就不能那样抓住例外。异常是在HTTPConnection()的构造函数中生成的,该构造函数中的大多数代码都已由不同的堆栈上下文包装:
因此,基本上每当在那里生成异常(在您的示例中为gaierro)时,它就已经被捕获并通过self.cleanup处理,从而生成599响应AFAICT:
不确定这是否能回答你的问题。
根据Tornado documentation:
如果在获取过程中发生错误,则给回调的HTTPResponse具有非None错误属性,该属性包含请求过程中遇到的异常。 您可以调用
response.rethrow()
在回调中抛出异常(如果有)。您在控制台上看到的消息只是一个警告(通过
logging.warning
发送)。它是无害的,但是如果它真的让您感到困扰,请参阅logging模块以了解如何过滤它。相关问题 更多 >
编程相关推荐