gpt4 book ai didi

python - Tornado如何返回错误异常?

转载 作者:太空宇宙 更新时间:2023-11-03 16:04:32 27 4
gpt4 key购买 nike

我想运行一个方法,我知道该方法不起作用,并且我想获取该方法返回的错误。

这是我的代码:

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)

这是我使用 Wring 端口运行程序时遇到的错误:

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)

当我输入错误的端口或主机时,我希望有 e。我怎样才能做到这一点?我厌倦了在 beanstalk = beanstalkt.Client(host='host', port=port) 之后添加 raise IOError("connection error") 但这会强制出现错误,并且我只想在错误存在时出现错误。

最佳答案

阅读代码在这里会有帮助。在beanstalkt 0.6的connect中,它创建一个IOStream来连接到服务器:

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

它会将您的回调注册为在成功时执行,但如果连接失败,它将永远每秒调用 Client._reconnect 一次。我认为您应该在他们的 GitHub 项目中打开一个功能请求,请求 connect 的错误通知系统。使用当前的 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()

关于python - Tornado如何返回错误异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39974225/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com