Jakob Kristensen
Jakob Kristensen

Reputation: 1757

Celery chaining parallel tasks into chord

I'm trying to wrap my head around this asynchronous task processing setup. So far I've been looking at using Celery, but am not locked on to anything yet. Only requirement is I can use Redis as broker and distribute the tasks on multiple nodes.

       ->  Task2  ->  Task3
Task1  ->  Task2  ->  Task3    [then]    Task4
       ->  Task2  ->  Task3

Explanation:

Question is then, how can i do this with Celery?

Upvotes: 4

Views: 2286

Answers (1)

Anton Pomieshchenko
Anton Pomieshchenko

Reputation: 2167

It can be done using chord and chain functions, please take a look of example. It should fit your needs.

from celery import Celery, chord, chain

backend = 'redis://redis:6379/'
app = Celery(result_backend=backend, backend=backend)


@app.task
def task1():
    argument = 123
    return chord([
        chain(task2.s(argument), task3.s()),
        chain(task2.s(argument), task3.s()),
        chain(task2.s(argument), task3.s()),
    ])(task4.s())


@app.task
def task2(argument):
    pass


@app.task
def task3(result_task2):
    pass


@app.task
def task4(result):
    pass


task1.apply_async()

Upvotes: 5

Related Questions