0
0
Flaskframework~10 mins

Calling tasks asynchronously in Flask - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Calling tasks asynchronously
Start Flask app
Receive HTTP request
Trigger async task
Return response immediately
Async task runs in background
Task completes and stores result
This flow shows how a Flask app triggers a background task asynchronously, returns a response immediately, and the task runs separately.
Execution Sample
Flask
from flask import Flask, request
from threading import Thread

app = Flask(__name__)

def background_task():
    print('Task running')

@app.route('/start-task')
def start_task():
    thread = Thread(target=background_task)
    thread.start()
    return 'Task started'

if __name__ == '__main__':
    app.run()
This code defines a Flask app with a route that starts a background task in a separate thread and returns a response immediately.
Execution Table
StepActionEvaluationResult
1Start Flask appApp ready to receive requestsApp running
2Receive HTTP requestRequest received at /start-taskTrigger async task
3Trigger async taskStart Thread(target=background_task)Thread started, task runs in background
4Return responseReturn 'Task started' immediatelyClient gets response quickly
5Background task runsPrint 'Task running'Message printed in console
6Task completesThread finishes executionBackground task done
💡 After task completes, Flask app continues running and can handle more requests.
Variable Tracker
VariableStartAfter Step 3After Step 5Final
appFlask instance createdRunningRunningRunning
threadNoneThread object created and startedThread runningThread finished
responseNoneNoneNone'Task started' sent to client
Key Moments - 2 Insights
Why does the client get a response immediately even though the task is still running?
Because the task runs in a separate thread (Step 3), Flask returns the response right away (Step 4) without waiting for the task to finish.
What happens if the background task takes a long time?
The client is not blocked and gets the response immediately. The task continues running in the background (Step 5) until it finishes (Step 6).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what happens at Step 3?
AThe Flask app stops running
BThe client receives the final result
CA background thread is started to run the task
DThe HTTP request is ignored
💡 Hint
Check Step 3 in the execution_table where the thread is started.
At which step does the client receive the response?
AStep 4
BStep 3
CStep 2
DStep 6
💡 Hint
Look for the step where 'Return response' happens in the execution_table.
If the background task prints a message, when does this happen?
ABefore the response is sent
BAfter the response is sent
CAt the same time as the response
DIt never prints
💡 Hint
See Step 5 in the execution_table where the task prints the message.
Concept Snapshot
Calling tasks asynchronously in Flask:
- Use a separate thread or worker to run long tasks
- Trigger the task inside the request handler
- Return HTTP response immediately
- Background task runs without blocking the client
- Useful for sending emails, processing files, etc.
Full Transcript
This visual trace shows how a Flask app handles asynchronous tasks. When the app receives a request, it starts a background thread to run a task. The app immediately returns a response to the client without waiting. The background task runs separately and prints a message when running. This approach keeps the app responsive and avoids blocking users while tasks run.