Bird
Raised Fist0
Flaskframework~20 mins

Task queue concept in Flask - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Task Queue Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate
2:00remaining
What happens when a Flask app uses a task queue?

Consider a Flask app that delegates long-running tasks to a task queue like Celery. What is the main benefit of this approach?

AThe Flask app will run tasks faster by using multiple CPU cores automatically.
BThe Flask app can respond quickly to user requests without waiting for the task to finish.
CThe Flask app will store all task results in the browser cache for faster access.
DThe Flask app will block user requests until the task queue finishes all tasks.
Attempts:
2 left
💡 Hint

Think about how task queues help with long tasks and user experience.

📝 Syntax
intermediate
2:00remaining
Which code snippet correctly sends a task to a Celery queue in Flask?

Given a Celery instance named celery and a task function add, which code sends the task asynchronously?

Flask
from celery import Celery
celery = Celery('app')

@celery.task
def add(x, y):
    return x + y
Aadd.delay(4, 6)
Badd.run(4, 6)
Cadd.apply(4, 6)
Dadd.call(4, 6)
Attempts:
2 left
💡 Hint

Look for the method that queues the task for background execution.

❓ state_output
advanced
2:00remaining
What is the output of this Flask + Celery task status check?

Assume a Celery task was sent with result = add.delay(2, 3). What will result.ready() return immediately after sending?

Flask
result = add.delay(2, 3)
print(result.ready())
AFalse
BTrue
CNone
DRaises AttributeError
Attempts:
2 left
💡 Hint

Think about whether the task finishes instantly or not.

🔧 Debug
advanced
2:00remaining
Why does this Flask + Celery task never complete?

Given this code, the task never finishes. What is the likely cause?

from celery import Celery
celery = Celery('app')

@celery.task
def multiply(x, y):
    return x * y

result = multiply.delay(5, 7)
print(result.get(timeout=1))
AThe task function multiply has a syntax error.
BThe get method does not accept a timeout argument.
CThe delay method is not valid for Celery tasks.
DNo Celery worker process is running to execute the task.
Attempts:
2 left
💡 Hint

Check if the background worker is active to process tasks.

🧠 Conceptual
expert
2:00remaining
Which statement best describes task queues in Flask apps?

Choose the most accurate description of how task queues improve Flask app design.

AThey replace Flask's routing system to manage HTTP requests more efficiently.
BThey automatically convert Flask apps into multi-threaded servers without extra configuration.
CThey allow Flask apps to handle heavy or slow tasks asynchronously, improving user experience and scalability.
DThey store all user session data in a centralized queue for faster retrieval.
Attempts:
2 left
💡 Hint

Think about the role of background processing in web apps.

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

  1. Step 1: Understand task queue role

    Task queues let slow jobs run separately from the main app, so the app stays responsive.
  2. Step 2: Identify the correct purpose

    Running slow tasks in the background matches the main purpose of task queues.
  3. Final Answer:

    To run slow or long tasks in the background without blocking the main app -> Option B
  4. 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

  1. Step 1: Recall rq enqueue method

    In rq, tasks are added to the queue using the enqueue() method.
  2. Step 2: Match method to options

    Only queue.enqueue(my_task, arg1, arg2) uses enqueue() correctly with task and arguments.
  3. Final Answer:

    queue.enqueue(my_task, arg1, arg2) -> Option A
  4. 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

  1. Step 1: Understand rq job processing

    When a job is enqueued, it runs asynchronously. The result is not ready immediately.
  2. Step 2: Check job.result value right after enqueue

    Immediately after enqueue, job.result is None because the worker hasn't processed it yet.
  3. Final Answer:

    None -> Option A
  4. 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

  1. Step 1: Check rq worker role

    rq worker must be running to process tasks from the queue.
  2. Step 2: Identify missing worker

    If tasks never run, the worker process is likely not started.
  3. Final Answer:

    You forgot to start the rq worker process -> Option C
  4. 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

  1. Step 1: Setup Redis and rq Queue

    You must create a Redis connection and pass it to rq Queue to enqueue tasks properly.
  2. Step 2: Run rq worker separately

    The rq worker must be started manually in the terminal to process queued tasks.
  3. Final Answer:

    Create Redis connection, create rq Queue with it, enqueue task; run rq worker in terminal -> Option D
  4. Quick Check:

    Redis + rq Queue + worker process = correct setup [OK]
Hint: Redis + rq Queue + manual worker run = background tasks [OK]
Common Mistakes:
  • Calling task function directly instead of enqueueing
  • Assuming worker runs automatically
  • Using Flask threading instead of task queue
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