user10898133
user10898133

Reputation:

How can run multiple celery tasks in parallel?

I have a two tasks.

@app.task
def run1():
    while True:
        print('run1')
        time.sleep(5)
    return


@app.task
def run2():
    while True:
        print('run2')
        time.sleep(2)
    return

How can I run these two tasks at the same time from the same console, from one command (preferably with a different number of workers).

Upvotes: 5

Views: 9255

Answers (1)

Lord Elrond
Lord Elrond

Reputation: 16032

You need to use group:

The group primitive is a signature that takes a list of tasks that should be applied in parallel.

Example from django shell:

>>> from celery import group
>>> from myapp.tasks import run1, run2
>>>
>>> run_group = group(run1.s(), run2.s())
>>> run_group()
<GroupResult: 06b3e88b-6c10-4ba5-bb32-5005c82eedfe [cc734fbd-3531-45d1-8575-64f4eff35523, 
1075e822-a6e2-4c34-8038-369613ff687d]>

For more complex usage, see the docs on group.

Upvotes: 8

Related Questions