Celery & Django - does the main process wait for tasks to finish?

38 views Asked by At

I've just started work on a project that uses Django & Celery. I'm quite new to both. One of the endpoints has code that looks like this:

task_do_something.delay()
return Response(
            {"success": True},
            status=status.HTTP_200_OK
        )

task_do_something is a task that takes a while to complete. (It does not call any other Celery tasks.) Will the main process execute the return statement immediately after Celery queues the task? Or will it wait for the task to be complete before moving on to the next line?

Reading through the documentation, I'm inclined to think that the answer would be the first option (execute next statement after the task is queued), because Celery tasks are asynchronous. But I'd like to double-check.

1

There are 1 answers

0
yousof A.Asadi On

When working with Django and Celery, understanding how asynchronous tasks are handled is critical to creating efficient and functional web applications. Let’s dive deeper into the actions of the Celery tasks and how they interact with the main Django system.

In your scenario, you have a Django view endpoint that uses delay() to queue the Celery task task_do_something and then immediately returns a response to the client. Your concern is whether the main Django process will wait for the job to complete before moving on to the next line or continue executing it immediately after the job queue

Celery tasks are truly asynchronous in nature, which means that when you call delay() or apply_async(), the task is added to the celery concatenation and the main program flow continues without waiting for the task to complete instead celery executes the task dealing with independent application process primarily.

So in your case the main django task does not wait until task_do_something completes. It executes the next line of code, which is a return statement that responds to a client with a success status.

tthis behavior is useful for web applications because it keeps the main django server responsive to incoming requests even when there are long jobs to be done in the background By migrating these jobs to celery, you block a server basically the way to block it and ensure an easier user experience.

However, it is important to note that as the main django process queues the job and continues to execute, the client may not receive an immediate response indicating that the job is complete If the client needs to be notified when the job is completed you have to do any polling or.