Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/extjs/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Tornado中的异常处理_Python_Exception Handling_Tornado - Fatal编程技术网

Python Tornado中的异常处理

Python Tornado中的异常处理,python,exception-handling,tornado,Python,Exception Handling,Tornado,我正在尝试处理在AsyncClient中发生的异常。请按以下方式获取: 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('Excep

我正在尝试处理在
AsyncClient中发生的异常。请按以下方式获取


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()的构造函数中生成的,该构造函数中的大部分代码已经由不同的堆栈上下文包装:

    with stack_context.StackContext(self.cleanup):
        parsed = urlparse.urlsplit(_unicode(self.request.url))
        [...]
因此,基本上,每当在那里生成异常(在您的示例中是GAIRROR)时,它都会被捕获并通过self.cleanup进行处理,从而生成599响应AFAICT:

@contextlib.contextmanager
def cleanup(self):
    try:
        yield
    except Exception, e:
        logging.warning("uncaught exception", exc_info=True)
        self._run_callback(HTTPResponse(self.request, 599, error=e,
                            request_time=time.time() - self.start_time,
                            ))
不确定这是否回答了您的问题。

根据:

如果在获取过程中发生错误,则提供给回调的HTTPResponse具有非None error属性,该属性包含在请求过程中遇到的异常。 您可以调用
response.rethrow()
在回调中抛出异常(如果有)

from tornado.httpclient import AsyncHTTPClient
from tornado.httpclient import HTTPRequest
from tornado.stack_context import ExceptionStackContext
from tornado import ioloop

import traceback

def handle_exc(*args):
    print('Exception occured')
    return True

def handle_request(response):
    if response.error is not None:
        with ExceptionStackContext(handle_exc):
            response.rethrow()
    else:
        print('Handle request')

http_client = AsyncHTTPClient()

http_client.fetch('http://some123site.com', handle_request)
http_client.fetch('http://google.com', handle_request)

ioloop.IOLoop.instance().start()


您在控制台上看到的消息只是一条警告(通过
logging.warning
)发送)。它是无害的,但如果它真的困扰您,请参阅模块了解如何过滤它。

您是否能够捕获此异常?似乎一个异常没有被正确地引发。@Glaslos,不,我无法捕捉到它:(.我记得在使用tornado的过程中发生了一些很难捕捉到的异常。也许可以查看一些正在进行的问题…对不起,我有一段时间没有使用tornado了…谢谢。我也在查看_Connection和cleaup,但这是tornado的内部代码。至于我,应该有一些机制来捕捉类似于c中的异常ode。通过查看代码的结构,我认为没有办法不修补Tornado。具体来说,
Tornado
将日志记录到根处理程序。因此,如果要抑制它们,需要将处理程序/筛选器附加到根处理程序。