芹菜链不适用于 django

Celery chain not working on django

我的目的是顺序调用两个任务

任务 1 完成后调用任务 2。到目前为止,我一直在做这个

为了按顺序执行我的任务,我使用了在文档中找到的链式方法。但是我的任务不需要上一个任务的结果就可以像文档中说的那样工作。因此,我将 CELERY_IGNORE_RESULT = True 定义为链方法,在执行时不获取上一个任务的结果。

我的代码是这样的

task.py

@app.task
def task1(param):
     ...

@app.task
def task2(param):
    ....

views.py

from .task import task1, task2
from celery import chain

chain(task1.delay(identifier), task2.delay(identifier))().get()

每次尝试我都会遇到这个错误:

unsupported operand type(s) for |: 'AsyncResult' and 'AsyncResult'

有谁知道我该如何解决这个问题?

问题是……您应该改为链接 signatures

chain(task1.s(identifier), task2.s(identifier))().get()

来自文档:

The chain primitive lets us link together signatures so that one is called after the other, essentially forming a chain of callbacks.

有关详细信息,请参阅 http://docs.celeryproject.org/en/latest/userguide/canvas.html#canvas-designing-work-flows