简体   繁体   English

Tornado 如何返回错误异常?

[英]Tornado how to return error exception?

I want to run a method I know this method doesn't work and I want to get the error returned by the method.我想运行一个方法,我知道这个方法不起作用,我想得到该方法返回的错误。

This is my code :这是我的代码:

def is_connect(s):
    print("ok connection")
    print(s)
    ioloop.stop()


try:


    current_job_ready = 0
    print("ok1")
    beanstalk = beanstalkt.Client(host='host', port=port)

    print("ok1")
    beanstalk.connect(callback=is_connect)


    ioloop = tornado.ioloop.IOLoop.instance()
    ioloop.start()
    print("ok2")
except IOError as e:
    print(e)

And this is the error I have when I run my program with wring port :这是我使用 wring port 运行程序时遇到的错误:

WARNING:tornado.general:Connect error on fd 7: ECONNREFUSED
ERROR:tornado.application:Exception in callback <functools.partial object at 0x7f5a0eac6f18>
Traceback (most recent call last):
  File "/usr/local/lib/python2.7/dist-packages/tornado/ioloop.py", line 604, in _run_callback
    ret = callback()
  File "/usr/local/lib/python2.7/dist-packages/tornado/stack_context.py", line 275, in null_wrapper
    return fn(*args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/tornado/ioloop.py", line 619, in <lambda>
    self.add_future(ret, lambda f: f.result())
  File "/usr/local/lib/python2.7/dist-packages/tornado/concurrent.py", line 237, in result
    raise_exc_info(self._exc_info)
  File "/usr/local/lib/python2.7/dist-packages/tornado/gen.py", line 270, in wrapper
    result = func(*args, **kwargs)
TypeError: connect() takes exactly 1 argument (2 given)

I want to have e when I enter a false port or host.当我输入错误的端口或主机时,我想拥有 e。 How can I do this?我怎样才能做到这一点? I tired to add raise IOError("connection error") after beanstalk = beanstalkt.Client(host='host', port=port) But this force the error, and I just want to have error when it exist.我厌倦了在beanstalk = beanstalkt.Client(host='host', port=port) raise IOError("connection error")之后添加raise IOError("connection error")但这会强制错误,我只想在错误存在时出现错误。

Here's where reading the code helps.这是阅读代码有帮助的地方。 In beanstalkt 0.6's connect , it creates an IOStream to connect to the server:在 beanstalkt 0.6 的connect ,它创建了一个 IOStream 来连接到服务器:

https://github.com/nephics/beanstalkt/blob/v0.6.0/beanstalkt/beanstalkt.py#L108 https://github.com/nephics/beanstalkt/blob/v0.6.0/beanstalkt/beanstalkt.py#L108

It registers your callback to be executed on success, but if the connection fails it'll just call Client._reconnect once per second forever.它注册您的回调以在成功时执行,但如果连接失败,它将永远每秒调用一次Client._reconnect I think you should open a feature request in their GitHub project asking for an error-notification system for connect .我认为您应该在他们的 GitHub 项目中打开一个功能请求,要求为connect提供一个错误通知系统。 With the current beanstalkt implementation, you just have to decide how long you're willing to wait for success:使用当前的 beanstalkt 实现,您只需决定愿意等待成功的时间:

import sys
from datetime import timedelta

from tornado.ioloop import IOLoop

def is_connect(s):
    print("ok connection")
    print(s)
    loop.remove_timeout(timeout)
    # Do something with Beanstalkd....

def connection_failed():
    print(sys.stderr, "Connection failed!")
    # Could call IOLoop.stop() or just quit.
    sys.exit(1)

loop = IOLoop.current()
timeout = loop.add_timeout(timedelta(seconds=1), connection_failed)
beanstalk.connect(callback=is_connect)
loop.start()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM