2011-08-08 17 views

回答

6

不得不挖掘相當多的東西來解決這個問題,因爲它沒有以友好的方式暴露在python-gearman-API中。但是,您可以通過自行創建GearmanJobGearmanJobRequest的適當實例來解決此問題。

這裏是你如何能做到這一點的一個小例子:

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

希望!

+0

我還在github的python-gearman叉子中添加了一個方便的方法。我不認爲它會隨時在官方發佈,但補丁可以在這裏找到:https://github.com/matslindh/python-gearman/commit/983e97c5055f1ccf7059f00215cc6e026ebc1ba0 – MatsLindh

+0

謝謝。它看起來像我需要的。但我解決它通過使用memcache包裝來存儲狀態和作業的其他數據。 –