开发者

How to get status of Gearman Jobs by their uniq id?

开发者 https://www.devze.com 2023-03-27 00:03 出处:网络
I need to get status of Gearman jobs by these uniq id, not by ope开发者_开发问答n handlers, as desribed every place I seen

I need to get status of Gearman jobs by these uniq id, not by ope开发者_开发问答n handlers, as desribed every place I seen

Is it possible? using in python-gearman v. 2...

Thanks for assistance!


Had to dig quite a bit to solve this issue, as it's not exposed in a friendly manner in the python-gearman-API. You can however solve it by creating appropriate instances of the GearmanJob and GearmanJobRequest yourself.

Here's a small example of how you can do this:

import gearman

client = gearman.GearmanClient(['localhost'])
result = client.submit_job('reverse', 'this is a string', background=True);

You want to keep track of which server got to handle the job (if you have more than one Gearman server handling tasks), and the handle of the task. The connection information is available through result.job.connection (.gearman_host and .gearman_port), while the handle is available through result.job.handle.

To check the status of a currently running job you create a GearmanClient, but only supply the server you want to query for the current state:

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))

Hopefully that helps!

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号