📖 Scenario: You are building a simple Flask web app that processes tasks in the background using a task queue. This helps the app stay responsive while doing work like sending emails or processing data.
🎯 Goal: Create a Flask app with a task queue list. Add tasks to the queue and process them one by one.
📋 What You'll Learn
Create a Flask app instance
Create a list called task_queue to hold tasks
Add a configuration variable MAX_TASKS to limit queue size
Write a function to add tasks to task_queue if under MAX_TASKS
Write a function to process and remove tasks from task_queue
💡 Why This Matters
🌍 Real World
Task queues help web apps handle slow or heavy work in the background, keeping the app fast and responsive for users.
💼 Career
Understanding task queues is important for backend developers working with web frameworks like Flask to build scalable and efficient applications.
Progress0 / 4 steps
1
Create Flask app and task queue list
Create a Flask app instance called app and a list called task_queue that starts empty.
Flask
Hint
Use Flask(__name__) to create the app. Create an empty list named task_queue.
2
Add configuration variable for max tasks
Add a configuration variable called MAX_TASKS to app.config and set it to 5.
Flask
Hint
Use app.config['MAX_TASKS'] = 5 to set the max tasks allowed in the queue.
3
Write function to add tasks to the queue
Write a function called add_task that takes a task string. It adds task to task_queue only if the length of task_queue is less than app.config['MAX_TASKS'].
Flask
Hint
Check the length of task_queue before adding the new task.
4
Write function to process and remove tasks
Write a function called process_task that removes and returns the first task from task_queue if the queue is not empty. If empty, return None.
Flask
Hint
Use pop(0) to remove the first task from the list.
Practice
(1/5)
1. What is the main purpose of using a task queue in a Flask application?
easy
A. To serve static files faster
B. To run slow or long tasks in the background without blocking the main app
C. To handle user authentication securely
D. To speed up the Flask app by caching all responses
Solution
Step 1: Understand task queue role
Task queues let slow jobs run separately from the main app, so the app stays responsive.
Step 2: Identify the correct purpose
Running slow tasks in the background matches the main purpose of task queues.
Final Answer:
To run slow or long tasks in the background without blocking the main app -> Option B
Quick Check:
Task queue purpose = background slow jobs [OK]
Hint: Task queues handle slow jobs outside main app flow [OK]
Common Mistakes:
Confusing task queues with caching
Thinking task queues handle authentication
Assuming task queues speed up static file serving
2. Which of the following is the correct way to enqueue a task using rq in Flask?
easy
A. queue.enqueue(my_task, arg1, arg2)
B. queue.run(my_task, arg1, arg2)
C. queue.start(my_task, arg1, arg2)
D. queue.execute(my_task, arg1, arg2)
Solution
Step 1: Recall rq enqueue method
In rq, tasks are added to the queue using the enqueue() method.
Step 2: Match method to options
Only queue.enqueue(my_task, arg1, arg2) uses enqueue() correctly with task and arguments.
Final Answer:
queue.enqueue(my_task, arg1, arg2) -> Option A
Quick Check:
rq enqueue method = enqueue() [OK]
Hint: Use enqueue() to add tasks to rq queue [OK]
Common Mistakes:
Using run(), start(), or execute() instead of enqueue()
Forgetting to pass task arguments
Calling enqueue on wrong object
3. Given this Flask rq task code snippet:
from rq import Queue
from redis import Redis
redis_conn = Redis()
queue = Queue(connection=redis_conn)
def add(x, y):
return x + y
job = queue.enqueue(add, 3, 4)
print(job.result)
What will be printed immediately after running this code?
medium
A. None
B. 7
C. Error: job.result not available
D. 3 + 4
Solution
Step 1: Understand rq job processing
When a job is enqueued, it runs asynchronously. The result is not ready immediately.
Step 2: Check job.result value right after enqueue
Immediately after enqueue, job.result is None because the worker hasn't processed it yet.
Final Answer:
None -> Option A
Quick Check:
job.result before worker runs = None [OK]
Hint: job.result is None until worker finishes task [OK]
Common Mistakes:
Assuming job.result is ready immediately
Expecting synchronous return value
Confusing job.result with function return
4. You wrote this code to enqueue a task in Flask with rq:
queue.enqueue(my_task, arg1, arg2)
But the task never runs. What is the most likely reason?
medium
A. Redis server is not installed
B. The task function my_task is missing
C. You forgot to start the rq worker process
D. You used enqueue instead of run
Solution
Step 1: Check rq worker role
rq worker must be running to process tasks from the queue.
Step 2: Identify missing worker
If tasks never run, the worker process is likely not started.
Final Answer:
You forgot to start the rq worker process -> Option C
Quick Check:
Worker must run to process tasks [OK]
Hint: Always run rq worker to process queued tasks [OK]
Common Mistakes:
Assuming enqueue runs task immediately
Not starting rq worker process
Confusing Redis installation with worker running
5. You want to run a slow image processing function in the background using Flask and rq. Which combination correctly sets up the task queue and worker?
hard
A. Install Redis, but only enqueue tasks; worker runs automatically without command
B. Create Flask app, call task function directly; no need for Redis or worker
C. Use Flask's built-in threading to run task; no Redis or rq needed
D. Create Redis connection, create rq Queue with it, enqueue task; run rq worker in terminal
Solution
Step 1: Setup Redis and rq Queue
You must create a Redis connection and pass it to rq Queue to enqueue tasks properly.
Step 2: Run rq worker separately
The rq worker must be started manually in the terminal to process queued tasks.
Final Answer:
Create Redis connection, create rq Queue with it, enqueue task; run rq worker in terminal -> Option D