如何使用第二个任务组中第一个任务的返回值进行循环?

时间:2016-07-15 19:43:35

标签: python celery chain

我有两个芹菜任务:

@app.task
def task1(a, b, c, d):
    # do some stuff and find the return value
    return r

@app.task
def task2(a, b, c, d, e, f, g):
   # do some other stuff

我想首先执行task1然后并行执行一组task2:

c = chain(task1.s(a, b, c, d), group(task2.si(a, b, c, e, i, j) for i, j in enumerate(range(e))))

但是,上面的“e”参数是task1的返回值,它被传递给task2并在for循环中使用。怎么能用Celery实现呢?

1 个答案:

答案 0 :(得分:0)

您可以为第一个任务指定基础,并使用第一个任务的retVal调用第二个任务。您可以在基础的on_success方法中执行此操作。

import Task from Celery

def setVal():
    return 1, 2, 3

class FollowUp(Task):
    def on_success(self, retval, task_id, *args, **kwargs):

        for i, j in enumerate(range(retval)):
            task2.si(a, b, c, retval, i, j) # a, b, c should be set before here
        # Use a task set here if you need to collectively verify the status of your tasks
        # http://docs.celeryproject.org/en/2.1-archived/reference/celery.task.sets.html


@app.task(base=FollowUpTask)
def add(*args):
    return 3


@app.task
def task2(a, b, c, d, e, f, g):
    pass