连接到 gearman 守护程序时,如果守护程序 url 或端口不正确并且无法建立连接,则会引发异常:
File "/usr/lib/python2.7/dist-packages/gearman/client.py", line 205, in establish_request_connection
raise ServerUnavailable('Found no valid connections: %r' % self.connection_list)
gearman.errors.ServerUnavailable: Found no valid connections: [<GearmanConnection localhost:4700 connected=False>]
我想捕捉异常并优雅地处理它,但下面的代码并没有这样做。显示异常和回溯,就好像我没有尝试捕获异常一样。
生成并尝试捕获异常的代码是:
import gearman
from gearman.errors import ConnectionError, InvalidAdminClientState, ServerUnavailable
try:
gmClient = gearman.GearmanClient(['localhost:4730'])
except gearman.errors.ServerUnavailable, e:
# I've also tried except ServerUnavailable, e: - same result.
print(e)
如何正确捕获 gearman 客户端连接异常?