0
0
Flaskframework~10 mins

Defining Celery tasks in Flask - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Defining Celery tasks
Start Flask app
Import Celery
Create Celery instance
Define task function with @app.task
Call task asynchronously
Worker picks task
Task runs in background
Result stored or returned
This flow shows how a Celery task is defined and executed asynchronously in a Flask app.
Execution Sample
Flask
from celery import Celery
app = Celery('tasks', broker='redis://localhost', backend='redis://localhost')

@app.task
def add(x, y):
    return x + y

result = add.delay(4, 6)
Defines a Celery task 'add' and calls it asynchronously with arguments 4 and 6.
Execution Table
StepActionEvaluationResult
1Import CeleryCelery class importedCelery available
2Create Celery instanceapp = Celery('tasks', broker='redis://localhost', backend='redis://localhost')Celery app ready
3Define task with @app.taskFunction 'add' wrapped as taskTask 'add' registered
4Call add.delay(4, 6)Task call queued asynchronouslyAsync task message sent
5Worker picks taskWorker receives task messageTask ready to run
6Task runs: add(4, 6)Function executesReturns 10
7Result storedResult saved in backendResult 10 available
8Main program continuesNo waiting for taskProgram not blocked
💡 Task completes after worker runs function and stores result asynchronously.
Variable Tracker
VariableStartAfter Step 4After Step 6Final
appNoneCelery instanceCelery instanceCelery instance
addFunctionTask objectTask objectTask object
resultNoneAsyncResult object (pending)AsyncResult object (ready)AsyncResult object (ready)
Key Moments - 3 Insights
Why doesn't calling add.delay(4, 6) run the function immediately?
Because add.delay queues the task to run asynchronously by a worker, so the main program continues without waiting. See execution_table step 4 and 8.
What does the @app.task decorator do?
It wraps the function so Celery knows it is a task to run asynchronously. See execution_table step 3.
How is the result of the task accessed?
The result is stored by Celery backend and can be accessed via the AsyncResult object returned by delay(). See execution_table step 7.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the state of 'result' after step 4?
ANone
BFunction return value
CAsyncResult object (pending)
DTask function itself
💡 Hint
Check variable_tracker column 'After Step 4' for 'result'
At which step does the worker actually run the task function?
AStep 4
BStep 6
CStep 5
DStep 7
💡 Hint
See execution_table where function executes and returns value
If you remove @app.task decorator, what changes in the execution?
Aadd.delay() call will fail or not queue task
BTask runs synchronously immediately
CResult is stored automatically
DWorker picks task faster
💡 Hint
Look at step 3 where task registration happens
Concept Snapshot
Defining Celery tasks in Flask:
- Import Celery and create instance with broker
- Use @app.task decorator on function
- Call task asynchronously with .delay(args)
- Worker runs task in background
- Result stored for later retrieval
Key: Tasks run outside main app flow asynchronously.
Full Transcript
This visual trace shows how to define and run Celery tasks in a Flask application. First, Celery is imported and an instance is created with a broker URL. Then, a function is decorated with @app.task to mark it as a Celery task. Calling the task with .delay() queues it asynchronously without blocking the main program. A worker process picks up the task, runs the function, and stores the result. The main app can continue running while the task executes in the background. The result can be accessed later through the AsyncResult object returned by .delay(). This approach helps run time-consuming tasks outside the main request flow.