简体   繁体   English

芹菜链任务用例?

[英]Celery chain task use case?

Suppose functions a, b, ..., that must be run sequentially. 假设必须依次运行函数a,b,...。 We can make a task that does that: 我们可以执行以下任务:

def a(...):
    ...
def b(...):
    ...
...
@app.task
def abc(...):
    a(...)
    b(...)
    ...

Alternatively, we can make them separate tasks: 另外,我们可以将它们单独设置为任务:

@app.task
def a(...):
    ...
@app.task
def b(...):
    ...
...

and chain them: 并将它们链接:

@app.task
def abc(...):
    chain(a.s(...), b.s(...), ...)
    ...

The first version is faster that the second (maybe not by much, depends on the task overhead vs. the average time spent in a, b, ...). 第一个版本比第二个版本快(可能不是很多,取决于任务开销与a,b,...中花费的平均时间)。 I suppose there must be a reason for chain but I haven't figured it out. 我想一定有一个chain的原因,但我还没有弄清楚。

There are several use cases: 有几种用例:

  1. You just want your tasks to be composable by the celery client rather than predefining workflows in the celery code base (ie, you can use chain on the client to submit tasks!) 您只希望您的任务可由celery客户端组成,而不是在celery代码库中预定义工作流(即,您可以使用客户端上的链来提交任务!)
  2. You want to monitor each subtask independently. 您要独立监视每个子任务。 Because chain launches each subtask as a separate task, each one can be monitored independently of the other. 由于链将每个子任务作为单独的任务启动,因此可以独立地监视每个子任务。 If you have one function that calls the other two as python functions, you can't hook into celery monitoring. 如果您有一个函数将其他两个函数作为python函数调用,则您将无法加入芹菜监控。
  3. You want to specialize workers/servers so that task A of the chain runs on one type of server and task B of the chain runs on another (eg for security). 您想使工作人员/服务器专业化,以便链的任务A在一种类型的服务器上运行,链的任务B在另一种类型的服务器上运行(例如,出于安全性考虑)。

The list can go on, but I've definitely used all three in production environments. 清单可以继续,但是我肯定在生产环境中使用了这三个。 Good luck! 祝好运!

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

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