0
0
Flaskframework~10 mins

Task queue concept in Flask - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Task queue concept
Client sends request
Add task to queue
Worker picks task
Worker processes task
Worker updates status/result
Client checks result/status
This flow shows how a task is added to a queue, processed by a worker, and the result is updated for the client.
Execution Sample
Flask
from flask import Flask
from rq import Queue
from redis import Redis

app = Flask(__name__)
redis_conn = Redis()
queue = Queue(connection=redis_conn)

def background_task(n):
    return n * 2

@app.route('/start/<int:n>')
def start_task(n):
    job = queue.enqueue(background_task, n)
    return f'Task {job.id} started'
This Flask app adds a background task to a Redis queue and returns the task ID.
Execution Table
StepActionQueue StateWorker StateOutput/Result
1Client calls /start/5EmptyIdleTask added to queue with ID 1
2Task 1 enqueuedTask 1 (background_task(5))IdleWaiting for worker
3Worker picks Task 1EmptyProcessing Task 1Task 1 started
4Worker runs background_task(5)EmptyProcessing Task 1Calculating 5 * 2
5Worker finishes Task 1EmptyIdleResult 10 stored
6Client can check resultEmptyIdleResult 10 available
💡 Task processed and result stored; queue is empty again.
Variable Tracker
VariableStartAfter Step 1After Step 3After Step 5Final
queueEmptyTask 1 addedTask 1 removedEmptyEmpty
worker_stateIdleIdleProcessing Task 1IdleIdle
task_resultNoneNoneNone1010
Key Moments - 2 Insights
Why does the client get a task ID immediately before the task finishes?
Because the task is added to the queue instantly (see Step 2), the client receives the ID right away while the worker processes it later (Step 3-5).
What happens if the worker is busy when a new task is added?
The new task stays in the queue (Step 2) until the worker finishes current tasks and picks it up (Step 3).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the worker state at Step 4?
AIdle
BProcessing Task 1
CWaiting for task
DTask completed
💡 Hint
Check the 'Worker State' column at Step 4 in the execution table.
At which step does the queue become empty again?
AStep 2
BStep 3
CStep 5
DStep 6
💡 Hint
Look at the 'Queue State' column to see when tasks are removed.
If the client sends two tasks quickly, how would the queue state change at Step 2?
AQueue has two tasks waiting
BQueue is empty
CWorker processes both tasks immediately
DQueue has one task only
💡 Hint
Refer to the queue behavior in the variable_tracker and execution_table for multiple tasks.
Concept Snapshot
Task queue lets Flask handle long jobs outside request time.
Client adds task to queue, gets task ID immediately.
Worker runs tasks one by one from queue.
Results stored separately for client to check later.
This keeps app responsive and scalable.
Full Transcript
In Flask, a task queue helps run long jobs in the background. When a client sends a request, the app adds the job to a queue and returns a task ID right away. A worker process picks tasks from the queue and runs them separately. This way, the app stays fast and doesn't wait for the job to finish. The worker stores the result, which the client can check later. The execution table shows each step: adding the task, worker picking it, processing, and finishing. Variables like queue state and worker state change as tasks move through the system. This method is common to keep web apps responsive when doing heavy work.