如果我正确理解了教程,Celery子任务支持与task几乎相同的 API ,但还有一个额外的优势,它可以传递给其他函数或进程。
显然,如果是这种情况,Celery 将简单地用子任务替换任务而不是保留两者(例如,装饰器会将函数转换为子任务而不是任务等)。所以我一定是误会了什么。@app.task
有什么可以一个任务做,一个子任务不能?
Celery API 发生了相当大的变化;我的问题特定于 3.1 版(目前是最新的)。
编辑:
我知道文档说子任务旨在从其他任务中调用。我的问题是什么阻止 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
Run Code Online (Sandbox Code Playgroud)
当您开始使用celery 的复杂工作流程时,您将考虑到这种差异。
\n\n\n\n\nSignature() 以某种方式包装单个任务调用的参数、关键字参数和执行选项,以便可以将其传递给函数,甚至可以序列化并通过网络发送。
\n\n签名通常被称为 \xe2\x80\x9csubtasks\xe2\x80\x9d 因为它们描述了要在任务中调用的任务。
\n
还:
\n\n\n\n\nsubtask\xe2\x80\x98s 是用于传递任务调用签名的对象(例如通过网络发送)
\n
Task只是一个用装饰器包装的函数定义,但是subtask是一个传递了参数但尚未开始的任务。您可以通过网络传输序列化的子任务,或者更常用的是在组/链/和弦中调用它。