简体   繁体   English

Celery 用一批消息执行任务

[英]Celery execute task with a batch of messages

I want to send messages to celery and when it reaches let's say 100 messages i want celery to execute them in batches.我想向 celery 发送消息,当它到达时,假设有 100 条消息,我希望 celery 批量执行它们。 This is a common scenario if I want to commit in batches to a database.如果我想批量提交到数据库,这是一个常见的场景。

For this purpose while googling around i found this link: for doing batches with celery: http://celery.readthedocs.org/en/latest/reference/celery.contrib.batches.html为此,我在谷歌搜索时找到了这个链接:用芹菜做批处理: http : //celery.readthedocs.org/en/latest/reference/celery.contrib.batches.html

My problem is that in the example there is no obvious way to get the data submitted to the task我的问题是在示例中没有明显的方法来获取提交给任务的数据

for instance lets say that we submit one by one some messages with:例如,假设我们一一提交了一些消息:

task.apply_async((message,), link_error=error_handler.s())

and then we have the following task implementation:然后我们有以下任务实现:

@celery.task(name="process.data", base=Batches, flush_every=100, flush_interval=1)
def process_messages(requests):
   for request in requests:
       print request /// how I can take the message data submitted in my task for process?

Is there any alternative way to achieve batches with celery?有没有其他方法可以用芹菜实现批量生产? Thank you谢谢

For anyone that will find this post useful after many trial and errors I have managed to take the data out of the SimplRequest object in the following way:对于经过多次试验和错误后会发现这篇文章有用的任何人,我设法通过以下方式从 SimplRequest 对象中取出数据:

When you submit your data with the following way:当您通过以下方式提交数据时:

func.delay(data)

from the request object you get the args attribute which is a list with the data:从请求对象你得到 args 属性,它是一个包含数据的列表:

request.args[0]
request.args[1] 
etc.

If you submit your data with the following way:如果您通过以下方式提交数据:

func.apply_async((), {'data': data}, link_error=error_handler.s())

then data are available as a dictionary in kwargs:然后数据可以作为 kwargs 中的字典使用:

request.kwargs['data']

Finally, as the example shows we need to do a loop into all requests to gather the data batch最后,如示例所示,我们需要对所有请求进行循环以收集数据批次

for r in requests:
       data = r.kwargs['data']

It would be nice for the examples in page of the documentation ( here ) to be updated with a more simple and clear example使用更简单明了的示例更新文档页面( 此处)中的示例会很好

The last version of batches.py available at https://github.com/celery/celery/blob/3.1/celery/contrib/batches.py before being deprecated doesn't work with Celery 5+ / Python 3.在被弃用之前https://github.com/celery/celery/blob/3.1/celery/contrib/batches.py 上可用的batches.py的最新版本batches.py用于 Celery 5+ / Python 3。

A working version can be found at https://gist.github.com/robin-vjc/1a4676ccb055162082c5a061ab556f58可以在https://gist.github.com/robin-vjc/1a4676ccb055162082c5a061ab556f58上找到工作版本

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM