0
0
Djangoframework~10 mins

Defining tasks in Django - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Defining tasks
Create tasks.py file
Define task function
Use @shared_task decorator
Task registered with Celery
Task can be called asynchronously
Task executes in worker process
This flow shows how to define a task in Django using Celery: create a tasks.py file, define a function, decorate it, register it, and then call it asynchronously.
Execution Sample
Django
from celery import shared_task

@shared_task
def add(x, y):
    return x + y
Defines a simple Celery task named 'add' that adds two numbers asynchronously.
Execution Table
StepActionCode LineResult
1Import shared_task decoratorfrom celery import shared_taskshared_task available
2Apply @shared_task decorator@shared_taskadd registered as Celery task
3Define function add(x, y)def add(x, y):Function add created
4Function body executes when task runsreturn x + yReturns sum of x and y
5Task can be called asynchronouslyadd.delay(4, 6)Task sent to worker for execution
6Worker executes taskadd(4, 6)Returns 10 asynchronously
💡 Task definition completes after decorator; execution happens later asynchronously.
Variable Tracker
VariableStartAfter Task CallAfter Execution
xundefined44
yundefined66
resultundefinedundefined10
Key Moments - 2 Insights
Why do we use @shared_task decorator instead of calling the function directly?
The @shared_task decorator registers the function as a Celery task so it can run asynchronously in a worker process, as shown in execution_table step 2 and 5.
When does the function body actually run?
The function body runs later in a separate worker process after calling add.delay(), not immediately when defining the function (see execution_table step 6).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what happens at step 3?
AThe function add is executed immediately
BThe task is sent to the worker
CThe function add is registered as a Celery task
DThe function add is deleted
💡 Hint
Check the 'Result' column at step 3 in the execution_table.
At which step is the task sent to the worker for execution?
AStep 5
BStep 4
CStep 2
DStep 6
💡 Hint
Look for 'Task sent to worker' in the execution_table.
If you remove the @shared_task decorator, what changes in the execution?
AThe function will be registered as a task automatically
BThe function can still run asynchronously
CThe function will run synchronously when called
DThe function will not be defined
💡 Hint
Refer to the key moment about the role of @shared_task decorator.
Concept Snapshot
Defining tasks in Django with Celery:
- Create tasks.py in app
- Define function with @shared_task decorator
- Call task asynchronously with .delay()
- Task runs in worker process
- Decorator registers function as async task
Full Transcript
To define a task in Django using Celery, you create a tasks.py file in your app. Inside, you define a function and decorate it with @shared_task. This decorator registers the function as a Celery task, allowing it to run asynchronously. When you call the task using .delay(), the task is sent to a worker process which executes the function body later. Variables like function arguments are passed to the worker, and the result is returned asynchronously. Without the decorator, the function runs synchronously when called.