I have a Django REST Framework project where I need to call two Celery tasks sequentially. Specifically, I need to call first_function
and then after some operations, call the second_function
, ensuring that second_function
runs only after first_function
has completed.
# tasks.py
from celery import shared_task
@shared_task
def first_function():
# Logic for the first function
@shared_task
def second_function():
# Logic for the second function
This is my view.py
first_function.delay()
# some codes ....
# now i need to call my second_function.delay()
chain(first_function.s(), second_function.s()).delay()
My concerns are:
- If multiple requests are made to the view simultaneously, will
second_function
correctly wait for the correspondingfirst_function
to complete? - I’m a little confused, How can I ensure that
second_function
runs after the specificfirst_function
related to the same request?(Note: I can’t add sleep or any blocking code in the middle of my code.)
Any guidance or best practices for handling this scenario with Celery would be greatly appreciated!