python - Celery 任务和子任务有什么区别?

标签 python celery

如果我对本教程的理解正确,Celery subtask 支持与 task 几乎相同的 API,但还有一个额外的优势,即它可以传递给其他函数或进程.

显然,如果是这种情况,Celery 会简单地将 tasks 替换为 subtasks 而不是保留两者(例如, @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

最佳答案

当您开始使用 complex workflows 时,您将考虑到差异。配 celery 。

A signature() wraps the arguments, keyword arguments, and execution options of a single task invocation in a way such that it can be passed to functions or even serialized and sent across the wire.

Signatures are often nicknamed “subtasks” because they describe a task to be called within a task.

还有:

subtask‘s are objects used to pass around the signature of a task invocation, (for example to send it over the network)

Task 只是一个用装饰器包裹的函数定义,而 subtask 是一个传递了参数但还没有开始的任务。您可以通过网络传输序列化的子任务,或者更常用的是,在组/链/弦内调用它。

关于python - Celery 任务和子任务有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39821099/

相关文章:

python - Elasticsearch-Python-具有刷新功能的批量助手API

python - 在python中顺序打开和关闭应用程序

python - Q : Python win32com: Code does not run after changing type library with makepy. py

python 文件返回为“没有这样的文件或目录”

python - 有人熟悉 Comfirmit 的数据格式吗?

python - celery 任务不会执行

python - PySQLPool 和 Celery,正确的使用方法?

python - Django celery : Passing request Object to worker

python - Celery 使用 SNS 发布消息

python - 如何使用 Celery 顺序执行独立任务?