Python Gearman 从后台任务获取数据

发布于 2024-12-19 09:58:12 字数 1756 浏览 2 评论 0原文

我尝试使用 gearman 执行后台任务,并从工作人员那里获取数据进度。 在文档中,我看到方法:send_job_data 和 send_job_status,但后台优先方法不起作用(我在 job.data_updates 中看不到数据),但 job.status 中的状态发生变化。

我将这段代码用于测试人员: 从 gearman 导入 GearmanWorker 导入时间

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

来自客户端的代码: 从 gearman 导入 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}

我如何正常获取因为我的后台任务将运行几个小时并在消息中发送有关我们状态的信息。

I'm try use gearman with backgroud tasks and get data progress from worker.
In documentation I'm see methods: send_job_data and send_job_status, but with background first method not work (I'm not see data in job.data_updates), but status changes in job.status.

I'm use this code for test workers:
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()

And this code from client:
from gearman import GearmanClient
client = GearmanClient(['192.168.1.79:4730'])

This code (blocking) work normal:

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}

And this client not work normal (not update status for task and not get data/result) :(

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}

How I'm can normal get this data? Because my background task will work few hours and send information about our status in messages.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

沐歌 2024-12-26 09:58:12

之所以这样称呼后台任务,是因为它们允许提交它们的客户端解除阻止并断开连接工作。他们不会向客户开放沟通渠道,因此您不会获得任何状态更新。它们本质上进入了比特桶。如果您希望后台任务传达其进度,则需要有其他渠道让它与感兴趣的程序进行通信。

如果您希望客户端继续运行并获取更新,但不阻止它们,您可以使用“任务”方法,在其中添加一堆任务,然后等待其中任何一个任务提供状态或完成。我不确定纯 python gearman 接口是否有这个,但 libgearman 接口有。它可以在此处以源形式获取 https://launchpad.net/gearman-interface 或在某些版本的 Ubuntu 中/Debian 作为 python-gearman.libgearman。

background tasks are called such because they allow the client that submitted them to un-block and work disconnected. They do not keep a channel of communication open to the client, so you won't get any of those status updates. They essentially go into the bit-bucket. If you want a background task to communicate its progress, you need to have some other channel for it to communicate with interested programs.

If you want the client to keep running and get updates, but not block on them, you can use the "task" method where you add a bunch of tasks, and then wait for any of them to provide status or be completed. I'm not sure if the pure python gearman interface has this, but the libgearman interface does. Its available in source form here https://launchpad.net/gearman-interface or in some versions of Ubuntu/Debian as python-gearman.libgearman.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文