0
0
Flaskframework~10 mins

Celery integration overview in Flask - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Celery integration overview
Start Flask app
Define Celery config
Create Celery instance
Define async task
Call task from Flask route
Task sent to Celery worker
Worker executes task
Result stored or returned
Flask app continues running
This flow shows how a Flask app sets up Celery, defines tasks, sends them to workers, and continues running.
Execution Sample
Flask
from flask import Flask
from celery import Celery

app = Flask(__name__)
celery = Celery(app.name, broker='redis://localhost:6379/0')

@app.route('/start-task')
def start_task():
    result = long_task.delay()
    return f'Task started: {result.id}'

@celery.task
def long_task():
    return 'Task done!'
This code sets up Flask and Celery, defines a long task, and starts it asynchronously from a route.
Execution Table
StepActionEvaluationResult
1Start Flask appFlask app instance createdApp ready to handle requests
2Configure CeleryCelery instance created with Redis brokerCelery ready to send tasks
3Define async taskTask function decorated with @celery.taskTask registered with Celery
4HTTP GET /start-taskstart_task() calledTask long_task sent to Celery worker
5Celery worker receives taskWorker executes long_task()Task returns 'Task done!'
6Result storedTask result saved in backendResult available for retrieval
7Flask route returnsReturns task id stringUser sees 'Task started: <id>'
8EndNo more actionsExecution stops here
💡 Execution stops after Flask route returns and Celery worker completes the task asynchronously.
Variable Tracker
VariableStartAfter Step 4After Step 5Final
appFlask instanceFlask instanceFlask instanceFlask instance
celeryCelery instanceCelery instanceCelery instanceCelery instance
resultNoneAsyncResult object with task idAsyncResult object with task resultAsyncResult object with task result
long_taskFunctionFunctionFunctionFunction
Key Moments - 3 Insights
Why does the Flask route return immediately before the task finishes?
Because the task is sent asynchronously to Celery workers (see execution_table step 4 and 7), Flask does not wait for task completion.
How does Celery know which broker to use?
The broker URL is set when creating the Celery instance (step 2), telling Celery where to send tasks.
What is the role of the Celery worker?
The worker listens for tasks (step 5), executes them, and stores results independently from Flask.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 4, what happens when the Flask route is called?
AThe task is executed immediately inside Flask
BThe task is sent asynchronously to Celery worker
CFlask waits for the task to finish before responding
DThe task is ignored
💡 Hint
Check execution_table step 4 and 7 where the task is sent and Flask returns immediately.
According to variable_tracker, what is the value of 'result' after step 5?
ANone
BAsyncResult object with task id only
CAsyncResult object with task result 'Task done!'
DString 'Task done!'
💡 Hint
Look at variable_tracker row for 'result' after step 5.
If the broker URL is changed, which step in execution_table is directly affected?
AStep 2 - Configure Celery
BStep 1 - Start Flask app
CStep 4 - HTTP GET /start-task
DStep 5 - Celery worker receives task
💡 Hint
Broker URL is set when creating Celery instance, see step 2.
Concept Snapshot
Celery integration with Flask:
- Create Flask app
- Create Celery instance with broker URL
- Define async tasks with @celery.task
- Call tasks with .delay() from Flask routes
- Celery workers run tasks asynchronously
- Flask returns immediately while tasks run in background
Full Transcript
This visual execution trace shows how to integrate Celery with Flask. First, the Flask app is created and ready to handle requests. Then, a Celery instance is configured with a broker URL, such as Redis. Next, asynchronous tasks are defined using the @celery.task decorator. When a Flask route calls a task using .delay(), the task is sent asynchronously to Celery workers. The Flask route returns immediately with the task id, while the Celery worker executes the task in the background and stores the result. Variables like the Flask app, Celery instance, and task result change state as the program runs. Key points include understanding asynchronous task execution, the role of the broker, and how workers process tasks independently from Flask. The included quiz questions help reinforce these concepts by referencing specific steps and variable states.