我正在尝试将 gearman 与背景任务一起使用,并从工作人员那里获取数据进度。在文档中,我看到了方法:send_job_data 和 send_job_status,但使用后台第一种方法不起作用(我在 job.data_updates 中看不到数据),但 job.status 中的状态发生了变化。
我将此代码用于测试人员: from gearman import GearmanWorker import time
worker = GearmanWorker(['192.168.1.79:4730'])
def long_task(work, job):
work.send_job_data(job, 'long task')
work.send_job_status(job, 0, 3)
time.sleep(60)
work.send_job_data(job, 'long task2')
work.send_job_status(job, 1,3)
time.sleep(120)
work.send_job_status(job,3,3)
return "COMPLETE ALL"
worker.register_task('pool', long_task)
worker.work()
来自客户端的这段代码: from gearman import GearmanClient client = GearmanClient(['192.168.1.79:4730'])
此代码(阻塞)工作正常:
In [6]: pool = client.submit_job('pool', '')
In [7]: pool.result
Out[7]: 'COMPLETE ALL'
In [8]: pool.data_updates
Out[8]: deque(['long task', 'long task2'])
In [9]: pool.status
Out[9]:
{'denominator': 3,
'handle': 'H:dhcp94:22',
'known': True,
'numerator': 3,
'running': True,
'time_received': 1322755490.691739}
而且这个客户端不能正常工作(不更新任务状态并且不获取数据/结果):(
In [10]: pool = client.submit_job('pool', '', background=True)
In [11]: pool = client.get_job_status(pool)
In [12]: pool.status
Out[12]:
{'denominator': 3,
'handle': 'H:dhcp94:23',
'known': True,
'numerator': 0,
'running': True,
'time_received': 1322755604.695123}
In [13]: pool.data_updates
Out[13]: deque([])
In [14]: pool = client.get_job_status(pool)
In [15]: pool.data_updates
Out[15]: deque([])
In [16]: pool.status
Out[16]:
{'denominator': 0,
'handle': 'H:dhcp94:23',
'known': False,
'numerator': 0,
'running': False,
'time_received': 1322755863.306605}
我怎样才能正常获取这些数据?因为我的后台任务将工作几个小时并在消息中发送有关我们状态的信息。