问题描述
我想运行一个方法,我知道这个方法不起作用,我想得到方法返回的错误。这是我的代码:
def is_connect(s)
打印(ok连接)
打印
ioloop.stop()
尝试:
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()
打印(ok2)
除了IOError作为e:
print(e)
这是我使用wring端口运行程序时出现的错误:
警告:tornado.general:连接错误fd 7:ECONNREFUSED
错误:tornado.application:回调中的异常< functools.partial对象在0x7f5a0eac6f18>
追溯(最近的最后一次呼叫):
文件/usr/local/lib/python2.7/dist-packages/tornado/ioloop.py,第604行,在_run_callback
ret = callback()
文件/usr/local/lib/python2.7/dist-packages/tornado/stack_context.py,第275行,null_wrapper
return fn(* args,** kwargs )
文件/usr/local/lib/python2.7/dist-packages/tornado/ioloop.py,第619行,< lambda>
self.add_future(ret,lambda f:f.result())
文件/usr/local/lib/python2.7/dist-packages/tornado/concurrent.py,第237行,结果
raise_exc_info(self._exc_info)
文件/usr/local/lib/python2.7/dist-packages/tornado/gen.py,第270行,包装器
结果= func(* args,** kwargs)
TypeError:connect()只需要一个参数(给定2)
当我输入错误的端口或主机时,我想要e。
我该怎么做?
我厌倦了添加在
beanstalk = beanstalkt.Client(host ='host',port =端口)
但这强制错误,我只想在出现错误时。
这里读取代码有帮助。在beanstalkt 0.6的连接
中,它创建一个IOStream连接到服务器:
它注册您的回调成功执行,但如果连接失败,它将调用 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)
#用Beanstalkd ....做一些事情
def connection_failed():
print(sys.stderr,连接失败!)
#可以调用IOLoop.stop()或只是退出。
sys.exit(1)
loop = IOLoop.current()
timeout = loop.add_timeout(timedelta(seconds = 1),connection_failed)
beanstalk。 connect(callback = is_connect)
loop.start()
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 :
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.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.
Here's where reading the code helps. In beanstalkt 0.6's connect
, it creates an IOStream to connect to the server:
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. I think you should open a feature request in their GitHub project asking for an error-notification system for connect
. With the current beanstalkt implementation, you just have to decide how long you're willing to wait for success:
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()
这篇关于龙卷风如何返回错误异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!