1

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

4

1 回答 1

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")
于 2014-03-21T14:50:33.120 回答