0
0
Flaskframework~10 mins

Task status monitoring in Flask - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Task status monitoring
Start Task
Task Running
Check Status Request
Return Current Status
Task Completed?
NoWait for Next Check
Yes
Return Final Status
End
This flow shows how a task starts, runs, and how status requests check and return the current state until completion.
Execution Sample
Flask
from flask import Flask, jsonify
app = Flask(__name__)
tasks = {}

@app.route('/start')
def start_task():
    tasks['task1'] = 'running'
    return jsonify({'status': 'started'})
This code starts a task and sets its status to 'running'.
Execution Table
StepActionTask Status BeforeTask Status AfterResponse Sent
1Start task1{}{'task1': 'running'}{"status": "started"}
2Check status of task1{'task1': 'running'}{'task1': 'running'}{"task1": "running"}
3Update task1 to completed{'task1': 'running'}{'task1': 'completed'}None
4Check status of task1{'task1': 'completed'}{'task1': 'completed'}{"task1": "completed"}
5No more status checks{'task1': 'completed'}{'task1': 'completed'}End monitoring
💡 Task status is 'completed', so monitoring ends.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4Final
tasks{}{'task1': 'running'}{'task1': 'running'}{'task1': 'completed'}{'task1': 'completed'}{'task1': 'completed'}
Key Moments - 2 Insights
Why does the task status not change when we only check it?
Checking the status (Step 2) only reads the current value without modifying it, so the status remains 'running' as shown in the execution_table row 2.
How do we know when to stop monitoring the task?
When the task status becomes 'completed' (Step 4), the monitoring ends as indicated in the exit_note and execution_table row 5.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the task status after Step 1?
A{}
B'completed'
C'running'
D'started'
💡 Hint
Check the 'Task Status After' column in row 1 of the execution_table.
At which step does the task status change to 'completed'?
AStep 3
BStep 2
CStep 4
DStep 5
💡 Hint
Look at the 'Task Status After' column to see when 'completed' first appears.
If we never update the task status to 'completed', what happens in the monitoring?
ATask status becomes 'failed'
BMonitoring continues indefinitely
CMonitoring ends immediately
DTask is removed from tasks dictionary
💡 Hint
Refer to the concept_flow where monitoring loops until status is 'completed'.
Concept Snapshot
Task status monitoring in Flask:
- Start task and set status (e.g., 'running')
- Provide endpoint to check current status
- Update status when task completes
- Continue checking until status is 'completed'
- Use a dictionary to track task states
- Return JSON responses with current status
Full Transcript
This example shows how to monitor a task's status in Flask. We start a task by setting its status to 'running' in a dictionary. When a client requests the status, the server returns the current state without changing it. Later, the task status is updated to 'completed'. The monitoring continues until the status is 'completed', at which point it stops. This process uses simple Flask routes and a dictionary to track task states, returning JSON responses to clients.