Challenge - 5 Problems
Async Flask Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2: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!'
Attempts:
2 left
💡 Hint
Think about how async functions and await work in Python and Flask's async support.
✗ Incorrect
Async Flask routes allow the server to handle other requests while waiting for awaitable operations like asyncio.sleep. So the server does not block but waits asynchronously.
📝 Syntax
intermediate2:00remaining
Correct async route declaration in Flask
Which of the following is the correct way to declare an async route in Flask 2.0+?
Attempts:
2 left
💡 Hint
Remember async functions must use await for async calls and return normal values.
✗ Incorrect
Option D correctly defines an async function with await inside and returns a string. Option D misses await, B is not async but uses await (syntax error), D tries to await a string (TypeError).
🔧 Debug
advanced2: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'
Attempts:
2 left
💡 Hint
What can you await in Python? Can you await a string?
✗ Incorrect
You can only await awaitable objects like coroutines or futures. Awaiting a string causes a TypeError.
❓ state_output
advanced2:00remaining
Output of async Flask route with shared state
Consider this Flask app with a shared counter:
What is the expected output after 3 quick requests to '/count'?
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)
Attempts:
2 left
💡 Hint
Think about what happens when multiple async requests read and write the same variable with a delay.
✗ Incorrect
Because of the await delay, multiple requests read the same counter value before any update, causing race conditions and repeated outputs.
🧠 Conceptual
expert2: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?
Attempts:
2 left
💡 Hint
Async routes should not block the event loop; how to handle blocking operations?
✗ Incorrect
To keep async routes efficient, use async database drivers or run blocking calls in separate threads to avoid blocking the event loop.