简体   繁体   English

Celery 动态添加任务到链

[英]Celery add task dynamically to chain

I am using celery 3 with Django.我在 Django 中使用 celery 3。
I have a list of jobs in database.我有一个数据库中的工作列表。 User can start a particular job which starts a celery task.用户可以启动启动芹菜任务的特定作业。
Now I want user to be able to start multiple jobs and it should add them to the celery queue and process them one after the other not in parallel as with async.现在我希望用户能够启动多个作业,它应该将它们添加到 celery 队列中并一个接一个地处理它们,而不是像异步那样并行处理。

I am trying to create a job scheduler with celery where user can select the jobs to execute and they will be executed in sequential fashion.我正在尝试使用 celery 创建一个作业调度程序,用户可以在其中选择要执行的作业,并且它们将按顺序执行。
If I use chain() then I cannot add new tasks to the chain dynamically.如果我使用chain()那么我不能动态地向链添加新任务。

What is ​the best solution?什么是最好的解决方案?

A better primitive for you to use would be link instead of chain in Celery.您可以使用的更好的原语是link而不是芹菜中的chain

From the documentation :文档

s = add.s(2, 2)
s.link(mul.s(4))
s.link(log_result.s())

You can see how this allows you to dynamically add a task to be executed by iterating through the required tasks in a loop, and linking each one's signature.您可以看到这如何允许您通过在循环中迭代所需任务并链接每个任务的签名来动态添加要执行的任务。 After the loop you would want to call something like s.apply_async(...) to execute them.在循环之后,您可能想要调用s.apply_async(...)类的东西来执行它们。

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

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