1

I need to execute secobd celery task after the first one is ready. The first task is already running and I have its id. How can I link another one to it?

# first task
first_task = change_timezone_geodata_task.delay(node.id, timezone, geodata_id)
task_id=  first_task .id
new task = change_timezone_geodata_task.delay(node2.id, timezone2, geodata_id2)

How to make new task execute after task ?

Aigerim Sadir
  • 353
  • 5
  • 18
  • Does this answer your question? [Python+Celery: Chaining jobs?](https://stackoverflow.com/questions/3901101/pythoncelery-chaining-jobs) – Tom Wojcik Feb 05 '21 at 09:40
  • No, I do not start them at the same time, the first task is already started by just 'delay' andI have its task_id – Aigerim Sadir Feb 05 '21 at 09:43
  • 1
    `Tasks can be linked together: the linked task is called when the task returns successfully:` The first line from the docs. You don't start them at the same time. – Tom Wojcik Feb 05 '21 at 09:57

2 Answers2

0

The first thing that comes to mind as solution to your problem is to rely on Celery's task linking capability.

DejanLekic
  • 18,787
  • 4
  • 46
  • 77
0

As you mentioned your task is running right now so by having id you can find out if your task is done or not it was from this answare

from celery.result import AsyncResult
from .tasks import app

res = AsyncResult('655890aa-4f02-467d-aaca-20004b70efe8d',app=app)
status = res.state # 'SUCCESS'

from .tasks import task2
if status == 'SUCCESS':
    task2.delay()

But if you want to run them after each other at the first you can use this:

If you don't want your callback to accept the result of the previous task as an argument, you can make your linked partial immutable by using si() instead of s()

(as mentioned in there)

from tasks import sum, increase_counter
​
sum.apply_async((8,8), link=increase_counter.si()) # Calculate 8 + 8 and increase counter of sum task calls
Sheracore
  • 21
  • 4