无法捕获Gearman Client实例化中的异常

时间:2013-08-13 06:26:50

标签: python-2.7 python-gearman

连接到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客户端连接异常?

1 个答案:

答案 0 :(得分:0)

尝试提交作业时会引发ServerUnavailable异常。试试这个:

import gearman

gmClient = gearman.GearmanClient(['localhost:4700'])

try:
    request = gmClient.submit_job('reverse', 'Hello World!')
except gearman.errors.ServerUnavailable, e:
    print("caught ServerUnavailable")