【发布时间】:2017-02-10 18:40:13
【问题描述】:
如果我对本教程的理解正确,Celery subtask 支持与 task 几乎相同的 API,但还有一个额外的优势,即它可以传递给其他函数或进程.
显然,如果是这样的话,Celery 会简单地将 tasks 替换为 subtasks 而不是保留两者(例如,@app.task 装饰器会转换一个函数子任务而不是任务等)。所以我一定是误会了什么。
任务能做什么而子任务不能?
Celery API 发生了很大变化;我的问题是针对 3.1 版(目前是最新版)。
编辑:
我知道文档说 subtasks 旨在从其他 tasks 中调用。我的问题是什么阻止 Celery 完全摆脱任务并在任何地方使用子任务?它们似乎比任务更灵活/更强大:
# tasks.py
from celery import Celery
app = Celery(backend='rpc://')
@app.task
def add(x, y):
# just print out a log line for testing purposes
print(x, y)
# client.py
from tasks import add
add_subtask = add.subtask()
# in this context, it seems the following two lines do the same thing
add.delay(2, 2)
add_subtask.delay(2, 2)
# when we need to pass argument to other tasks, we must use add_subtask
# so it seems add_subtask is strictly better than add
【问题讨论】:
-
我认为子任务是从任务运行的次要任务。 stackoverflow.com/questions/6349371/…
-
@Hussain 是的,准确地说。查看我更新的问题。