Python Tornado 中的异常处理
我试图以这种方式处理 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
我做错了什么?
I am trying to handle exception occurred in AsyncClient.fetch
in this way:
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()
and see next output:
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
What am I doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
根据 Tornado 文档:
如果在获取时,给回调的 HTTPResponse 有一个非 None 错误属性,其中包含请求期间遇到的异常。
您可以在回调中调用response.rethrow()来抛出异常(如果有)。
您在控制台上看到的消息只是一条警告(通过
logging.warning
发送)。它是无害的,但如果它确实困扰您,请参阅 logging 模块了解如何过滤它。According to the Tornado documentation:
If an error occurs during the fetch, the HTTPResponse given to the callback has a non-None error attribute that contains the exception encountered during the request.
You can call
response.rethrow()
to throw the exception (if any) in the callback.The message you're seeing on the console is only a warning (sent through
logging.warning
). It's harmless, but if it really bothers you, see the logging module for how to filter it.我根本不了解 Tornado,但我看了一下,你根本无法通过这种方式捕获异常。异常是在 _HTTPConnection() 的构造函数中生成的,并且该构造函数中的大部分代码已经被不同的堆栈上下文包装:
因此基本上每当在那里生成异常(在您的示例中为 gaierror )时,它都会被捕获并处理通过 self.cleanup,这又会生成 599 响应 AFAICT:
不确定这是否回答了您的问题。
I don't know Tornado at all, but I gave a look and you simply can't catch exceptions that way. The exception is generated in the constructor of _HTTPConnection(), and most of the code in that constructor is already wrapped by a different stack context:
So basically whenever an exception is generated there (gaierror in your example), it is caught already and handled through self.cleanup, that in turns generate a 599 response AFAICT:
Not sure if this answers your question.