如何通过唯一ID获取Gearman作业状态?
我需要通过这些唯一的ID来获取Gearman任务的状态,而不是通过打开的处理程序,正如我在各个地方看到的那样。
这样做可能吗?使用的是python-gearman版本2……
谢谢你的帮助!
1 个回答
6
为了搞定这个问题,我花了不少时间,因为在python-gearman-API里并没有很友好的说明。不过,你可以通过自己创建合适的 GearmanJob
和 GearmanJobRequest
实例来解决这个问题。
下面是一个简单的例子,教你怎么做:
import gearman
client = gearman.GearmanClient(['localhost'])
result = client.submit_job('reverse', 'this is a string', background=True);
你可能想知道哪个服务器在处理这个任务(如果你有多个Gearman服务器在处理任务的话),还有这个任务的句柄。连接信息可以通过 result.job.connection
获取(里面有 .gearman_host
和 .gearman_port
),而句柄则可以通过 result.job.handle
获取。
要检查当前正在运行的任务状态,你需要创建一个 GearmanClient
,但只需要提供你想查询的服务器,以获取当前状态:
client = gearman.GearmanClient(['localhost'])
# configure the job to request status for - the last four is not needed for Status requests.
j = gearman.job.GearmanJob(client.connection_list[0], result.job.handle, None, None, None, None)
# create a job request
jr = gearman.job.GearmanJobRequest(j)
jr.state = 'CREATED'
# request the state from gearmand
res = client.get_job_status(jr)
# the res structure should now be filled with the status information about the task
print(str(res.status.numerator) + " / " + str(res.status.denominator))
希望这能帮到你!