Bird
Raised Fist0
Flaskframework~10 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What is the main purpose of integrating Celery with a Flask application?
easy
A. To run tasks in the background without blocking the main app
B. To create HTML templates for the Flask app
C. To handle user authentication and sessions
D. To serve static files like images and CSS

Solution

  1. Step 1: Understand Celery's role

    Celery is designed to run tasks asynchronously, meaning it handles work in the background.
  2. Step 2: Connect to Flask app purpose

    Flask apps use Celery to keep the app responsive by offloading long tasks.
  3. Final Answer:

    To run tasks in the background without blocking the main app -> Option A
  4. Quick Check:

    Celery = background tasks [OK]
Hint: Celery = background task runner in Flask apps [OK]
Common Mistakes:
  • Thinking Celery manages templates
  • Confusing Celery with user login features
  • Assuming Celery serves static files
2. Which of the following is the correct way to define a Celery task in a Flask app?
easy
A. @celery_app.task\ndef add(x, y): return x + y
B. celery_app.run_task(add)
C. @app.route('/task')\ndef task(): pass
D. def task(): pass

Solution

  1. Step 1: Identify Celery task decorator

    Celery tasks are defined using the @celery_app.task decorator.
  2. Step 2: Check syntax correctness

    @celery_app.task\ndef add(x, y): return x + y correctly decorates a function to become a Celery task.
  3. Final Answer:

    @celery_app.task\ndef add(x, y): return x + y -> Option A
  4. Quick Check:

    Celery task = @celery_app.task decorator [OK]
Hint: Use @celery_app.task to define tasks [OK]
Common Mistakes:
  • Forgetting the @celery_app.task decorator
  • Using Flask route decorators for tasks
  • Calling celery_app.run_task which is invalid
3. Given this code snippet in a Flask app with Celery:
@celery_app.task
def multiply(x, y):
    return x * y

result = multiply.delay(3, 4)
print(result.get())
What will be printed?
medium
A. A task object reference
B. null
C. 12
D. An error because delay() is not valid

Solution

  1. Step 1: Understand delay() usage

    Calling multiply.delay(3, 4) sends the task to Celery to run asynchronously.
  2. Step 2: Using get() to wait for result

    result.get() waits for the task to finish and returns the output, which is 3 * 4 = 12.
  3. Final Answer:

    12 -> Option C
  4. Quick Check:

    delay() runs task, get() returns result [OK]
Hint: delay() runs task, get() fetches result [OK]
Common Mistakes:
  • Thinking delay() returns the result immediately
  • Confusing task object with result value
  • Assuming delay() is invalid in Celery
4. You wrote this Celery task in your Flask app:
@celery_app.task
def add(x, y):
return x + y
But when you call add.delay(2, 3), it raises an IndentationError. What is the problem?
medium
A. delay() cannot be called on tasks
B. The function body is not indented properly
C. You forgot to import Celery
D. The decorator syntax is incorrect

Solution

  1. Step 1: Check function indentation

    Python requires the function body to be indented under the def line.
  2. Step 2: Identify error cause

    Here, return x + y is not indented, causing IndentationError.
  3. Final Answer:

    The function body is not indented properly -> Option B
  4. Quick Check:

    Python needs indented function bodies [OK]
Hint: Indent function body under def line [OK]
Common Mistakes:
  • Ignoring Python indentation rules
  • Assuming delay() is invalid
  • Thinking decorator syntax is wrong
5. You want to integrate Celery with Flask using Redis as the broker. Which steps correctly describe the setup process?
hard
A. Install Celery only, define tasks without decorator, call tasks directly, no broker needed
B. Install Flask-Login, configure Celery with SQLite, define tasks with @app.route, call tasks with run()
C. Install Redis, configure Flask routes for tasks, use delay() without decorator, run Redis server
D. Install Redis, configure Celery with Redis URL, define tasks with @celery_app.task, call tasks with delay()

Solution

  1. Step 1: Setup Redis broker

    Redis must be installed and running as the message broker for Celery.
  2. Step 2: Configure Celery and define tasks

    Celery is configured with Redis URL, tasks use @celery_app.task, and are called with delay().
  3. Final Answer:

    Install Redis, configure Celery with Redis URL, define tasks with @celery_app.task, call tasks with delay() -> Option D
  4. Quick Check:

    Redis + @celery_app.task + delay() = correct setup [OK]
Hint: Use Redis broker, @celery_app.task, and delay() calls [OK]
Common Mistakes:
  • Using wrong broker like SQLite
  • Defining tasks without decorator
  • Calling tasks with run() or no broker
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard