如果前面的任务成功,芹菜会运行任务

时间:2021-06-15 19:18:08

I need to run four tasks but they need to run one after the other only if they are successful.

我需要运行4个任务,但是只有当它们成功时,它们才需要一个接一个地运行。

I have tried to chain them like this..but they start off independently

我试图把它们像这样捆起来。但一开始是独立的

res = (mul.si(5,5) | mul.si(5,6) | mul.si(5,7) | mul.si(5,8) | mul.si(5,9) )()

any idea?

任何想法?

1 个解决方案

#1


1  

Yes you can do it. But you need to store the result as mentioned here.

是的,你能做到。但是您需要存储这里提到的结果。

But this is rarely used since it turns the asynchronous call into a synchronous one

但这很少使用,因为它将异步调用转换为同步调用

In my example,

在我的例子中,

tasks.py is like

任务。py就像

from celery import Celery
import datetime

app = Celery('tasks',backend='amqp' broker='amqp://guest@localhost//')

@app.task
def add(x, y):
    return x + y


def add_chained(args_list=list()):
    for args in args_list:
        print "Performing addtion for %s at %s" % (args, datetime.datetime.now())
        result = add.delay(*args)
        while not result.ready():
            pass

Result is Like this:

结果是这样的:

>>> import tasks
>>> tasks.add_chained([(1,2), (2,3), (3,4), (4,5)])
Performing addtion for (1, 2) at 2014-01-17 18:49:57.392357
Performing addtion for (2, 3) at 2014-01-17 18:49:57.428961
Performing addtion for (3, 4) at 2014-01-17 18:49:57.432598
Performing addtion for (4, 5) at 2014-01-17 18:49:57.435891

#1


1  

Yes you can do it. But you need to store the result as mentioned here.

是的,你能做到。但是您需要存储这里提到的结果。

But this is rarely used since it turns the asynchronous call into a synchronous one

但这很少使用,因为它将异步调用转换为同步调用

In my example,

在我的例子中,

tasks.py is like

任务。py就像

from celery import Celery
import datetime

app = Celery('tasks',backend='amqp' broker='amqp://guest@localhost//')

@app.task
def add(x, y):
    return x + y


def add_chained(args_list=list()):
    for args in args_list:
        print "Performing addtion for %s at %s" % (args, datetime.datetime.now())
        result = add.delay(*args)
        while not result.ready():
            pass

Result is Like this:

结果是这样的:

>>> import tasks
>>> tasks.add_chained([(1,2), (2,3), (3,4), (4,5)])
Performing addtion for (1, 2) at 2014-01-17 18:49:57.392357
Performing addtion for (2, 3) at 2014-01-17 18:49:57.428961
Performing addtion for (3, 4) at 2014-01-17 18:49:57.432598
Performing addtion for (4, 5) at 2014-01-17 18:49:57.435891