Python龙卷风中的异常处理 [英] Exception handling in Python Tornado

查看:389
本文介绍了Python龙卷风中的异常处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试处理以 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()

与ExceptionStackContext(handle_exc):
http_client.fetch('http://some123site.com',handle_request)

ioloop.IOLoop.instance()。start()

看到下一个输出:

  
警告:root:未捕获的异常
追溯(最近的呼叫最后):
文件/home/crchemist/python-3.2/lib/python3.2/site-packages/tornado-2.0-py3.2.egg/tornado/simple_httpclient.py,第259行,清除nup
yield
文件/home/crchemist/python-3.2/lib/python3.2/site-packages/tornado-2.0-py3.2.egg/tornado/simple_httpclient.py,第162行,__init__
0,0)
socket.gaierror:[Errno -5]没有与主机名相关联的地址
处理请求

我做错了什么?

解决方案

龙卷风文档



如果在获取期间发生错误,则给予回调的HTTPResponse具有非无错误属性,其中包含请求期间遇到的异常。
您可以调用 response.rethrow()在回调中抛出异常(如果有)。



<从tornado.httpclient导入AsyncHTTPClient
从tornado.httpclient导入HTTPRequest
从tornado.stack_context导入ExceptionStackContext
从龙卷风导入ioloop

import traceback

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

def handle_request(response) :
如果response.error不是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 发送)。它是无害的,但是如果真的打扰你,请参阅日志模块,了解如何过滤它。


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?

解决方案

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.

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()


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.

这篇关于Python龙卷风中的异常处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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