0
0
Flaskframework~20 mins

Calling tasks asynchronously in Flask - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Async Task Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output when calling a Flask route that triggers a Celery task asynchronously?

Consider a Flask app with a Celery task defined as follows:

from flask import Flask, jsonify
from celery import Celery

app = Flask(__name__)
celery = Celery(app.name, broker='redis://localhost:6379/0')

@celery.task
def add(x, y):
    return x + y

@app.route('/add')
def call_add():
    task = add.delay(4, 5)
    return jsonify({'task_id': task.id})

What will the client receive when accessing the /add route?

Flask
from flask import Flask, jsonify
from celery import Celery

app = Flask(__name__)
celery = Celery(app.name, broker='redis://localhost:6379/0')

@celery.task
def add(x, y):
    return x + y

@app.route('/add')
def call_add():
    task = add.delay(4, 5)
    return jsonify({'task_id': task.id})
AA JSON response with the sum result, e.g., {'result': 9}
BA JSON response containing the task ID, e.g., {'task_id': 'some-uuid'}
CAn immediate integer 9 as plain text
DA server error because the task is not awaited
Attempts:
2 left
💡 Hint

Remember that delay() queues the task and returns immediately with a task ID.

state_output
intermediate
1:30remaining
What is the state of a Celery task immediately after calling delay()?

Given a Celery task process_data and calling task = process_data.delay(), what is the initial state of task.state?

Flask
from celery import Celery

celery = Celery('app', broker='redis://localhost:6379/0')

@celery.task
def process_data():
    return 'done'

task = process_data.delay()
A'PENDING'
B'SUCCESS'
C'FAILURE'
D'STARTED'
Attempts:
2 left
💡 Hint

Think about what happens right after a task is queued but before it runs.

📝 Syntax
advanced
2:30remaining
Which Flask route code correctly calls a Celery task asynchronously and returns the task result synchronously?

Choose the correct Flask route code that calls a Celery task asynchronously but waits for the result before returning it.

A
def route():
    task = add.delay(2, 3)
    return str(task.get())
B
def route():
    result = add(2, 3)
    return str(result)
C
def route():
    task = add.delay(2, 3)
    return jsonify({'result': task.state})
D
def route():
    task = add.apply_async((2, 3))
    return jsonify({'result': task.id})
Attempts:
2 left
💡 Hint

Look for the method that waits for the task result.

🔧 Debug
advanced
2:00remaining
Why does this Flask route never return a response when calling a Celery task asynchronously?

Given this Flask route:

@app.route('/longtask')
def longtask():
    result = long_running_task.delay()
    return result.get()

The route hangs and never returns. What is the most likely cause?

Flask
@app.route('/longtask')
def longtask():
    result = long_running_task.delay()
    return result.get()
AThe delay() method raises a syntax error.
BThe task is synchronous and blocks the Flask server.
CThe route returns before the task is queued.
DThe Celery worker is not running, so the task never completes.
Attempts:
2 left
💡 Hint

Check if the background worker is active.

🧠 Conceptual
expert
1:30remaining
Which statement best describes the advantage of calling tasks asynchronously in Flask with Celery?

Why do developers use Celery to call tasks asynchronously in Flask applications?

ATo make all Flask routes execute faster by running tasks synchronously.
BTo avoid using message brokers and simplify deployment.
CTo offload long-running or blocking tasks from the web server, improving responsiveness and scalability.
DTo ensure tasks run only when the Flask app is restarted.
Attempts:
2 left
💡 Hint

Think about what happens when a web server handles slow tasks directly.