0
0
Flaskframework~20 mins

Migrating to async Flask - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Async Flask Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
Understanding async route behavior in Flask
What will be the output behavior of this async Flask route when accessed?
from flask import Flask
import asyncio

app = Flask(__name__)

@app.route('/wait')
async def wait():
    await asyncio.sleep(1)
    return 'Done waiting!'
Flask
from flask import Flask
import asyncio

app = Flask(__name__)

@app.route('/wait')
async def wait():
    await asyncio.sleep(1)
    return 'Done waiting!'
AThe server waits 1 second asynchronously before responding with 'Done waiting!'
BThe server blocks all requests for 1 second before responding with 'Done waiting!'
CThe route raises a RuntimeError because async routes are not supported
DThe server immediately responds with 'Done waiting!' without delay
Attempts:
2 left
💡 Hint
Think about how async functions and await work in Python and Flask's async support.
📝 Syntax
intermediate
2:00remaining
Correct async route declaration in Flask
Which of the following is the correct way to declare an async route in Flask 2.0+?
A
@app.route('/data')
async def data():
    return 'Async data'
B
@app.route('/data')
def data():
    await asyncio.sleep(1)
    return 'Async data'
C
@app.route('/data')
async def data():
    return await 'Async data'
D
@app.route('/data')
async def data():
    await asyncio.sleep(1)
    return 'Async data'
Attempts:
2 left
💡 Hint
Remember async functions must use await for async calls and return normal values.
🔧 Debug
advanced
2:00remaining
Identify the error in this async Flask route
What error will this Flask async route raise when accessed?
from flask import Flask

app = Flask(__name__)

@app.route('/error')
async def error():
    return await 'Hello'
Flask
from flask import Flask

app = Flask(__name__)

@app.route('/error')
async def error():
    return await 'Hello'
ATypeError: object str can't be used in 'await' expression
BSyntaxError: 'await' outside async function
CRuntimeError: Async route not supported
DNo error, returns 'Hello' correctly
Attempts:
2 left
💡 Hint
What can you await in Python? Can you await a string?
state_output
advanced
2:00remaining
Output of async Flask route with shared state
Consider this Flask app with a shared counter:
from flask import Flask
import asyncio

app = Flask(__name__)
counter = 0

@app.route('/count')
async def count():
    global counter
    temp = counter
    await asyncio.sleep(0.1)
    counter = temp + 1
    return str(counter)

What is the expected output after 3 quick requests to '/count'?
Flask
from flask import Flask
import asyncio

app = Flask(__name__)
counter = 0

@app.route('/count')
async def count():
    global counter
    temp = counter
    await asyncio.sleep(0.1)
    counter = temp + 1
    return str(counter)
AOutputs: 1, 2, 3 (each request increments counter correctly)
BOutputs: 1, 1, 1 (race condition causes same value returned)
COutputs: 3, 3, 3 (counter increments before requests finish)
DRaises RuntimeError due to global variable access in async route
Attempts:
2 left
💡 Hint
Think about what happens when multiple async requests read and write the same variable with a delay.
🧠 Conceptual
expert
2:00remaining
Best practice for database calls in async Flask routes
When migrating a Flask app to async routes, what is the best approach to handle database calls that are blocking?
AReplace database calls with in-memory dictionaries to avoid blocking
BKeep using the blocking database calls directly inside async routes
CUse an async database driver or run blocking calls in a thread executor
DRemove async from routes and keep all calls blocking
Attempts:
2 left
💡 Hint
Async routes should not block the event loop; how to handle blocking operations?