Bird
Raised Fist0
Flaskframework~20 mins

Migrating to async Flask - Practice Problems & Coding Challenges

Choose your learning style10 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?

Practice

(1/5)
1. What is the main benefit of migrating a Flask route to use async def functions?
easy
A. It disables synchronous routes in the app.
B. It automatically fixes all bugs in the Flask app.
C. It makes the app use less memory by default.
D. It allows the app to handle multiple tasks at the same time, improving speed.

Solution

  1. Step 1: Understand async function purpose in Flask

    Async functions let Flask handle multiple requests or tasks without waiting for each to finish, improving concurrency.
  2. Step 2: Identify the benefit of concurrency

    Handling tasks concurrently means faster response times and better resource use.
  3. Final Answer:

    It allows the app to handle multiple tasks at the same time, improving speed. -> Option D
  4. Quick Check:

    Async = concurrency = better speed [OK]
Hint: Async means handling many tasks at once for faster apps [OK]
Common Mistakes:
  • Thinking async fixes bugs automatically
  • Believing async reduces memory without changes
  • Assuming async disables sync routes
2. Which of the following is the correct way to define an async route in Flask?
easy
A. async def async_route(): return 'Hello'
B. def async_route(): return 'Hello'
C. async def async_route(): await return 'Hello'
D. def async_route(): await 'Hello'

Solution

  1. Step 1: Recall async function syntax

    Async functions start with async def and can use await inside.
  2. Step 2: Check each option

    async def async_route(): return 'Hello' correctly defines an async function returning a string. def async_route(): return 'Hello' is sync. async def async_route(): await return 'Hello' misuses await with return. def async_route(): await 'Hello' uses await outside async.
  3. Final Answer:

    async def async_route(): return 'Hello' -> Option A
  4. Quick Check:

    Async function = async def [OK]
Hint: Async routes start with 'async def' keyword [OK]
Common Mistakes:
  • Using await outside async functions
  • Trying to await a return statement
  • Defining async routes without async def
3. Given this async Flask route, what will be the output when accessed?
from flask import Flask
import asyncio
app = Flask(__name__)

@app.route('/')
async def home():
    await asyncio.sleep(0.1)
    return 'Done'
medium
A. The route crashes because Flask does not support async.
B. The route returns immediately with 'Done'.
C. The route returns 'Done' after a short delay.
D. The route causes a syntax error due to await usage.

Solution

  1. Step 1: Understand async route behavior

    The route uses await asyncio.sleep(0.1) which pauses the function asynchronously for 0.1 seconds.
  2. Step 2: Predict output after await

    After the short async pause, the route returns the string 'Done'. So the client sees 'Done' after a slight delay.
  3. Final Answer:

    The route returns 'Done' after a short delay. -> Option C
  4. Quick Check:

    Await sleep delays then returns [OK]
Hint: Await pauses async route before returning result [OK]
Common Mistakes:
  • Thinking await causes syntax error
  • Expecting immediate return ignoring await
  • Assuming Flask can't run async routes
4. What is wrong with this async Flask route code?
from flask import Flask
app = Flask(__name__)

@app.route('/')
async def index():
    return 'Hello'
medium
A. Flask version might not support async routes causing runtime error.
B. Nothing is wrong; this is valid async Flask code.
C. Missing await keyword before return statement.
D. Async functions cannot return strings directly.

Solution

  1. Step 1: Check Flask async support requirements

    Async routes require Flask 2.0+; older versions do not support async and cause errors.
  2. Step 2: Analyze code correctness vs environment

    The code syntax is correct, but if Flask version is old, it will fail at runtime.
  3. Final Answer:

    Flask version might not support async routes causing runtime error. -> Option A
  4. Quick Check:

    Async needs Flask 2.0+ [OK]
Hint: Check Flask version supports async before using async routes [OK]
Common Mistakes:
  • Assuming all Flask versions support async
  • Thinking await is always required before return
  • Believing async can't return strings
5. You want to migrate a Flask app to async and have a route that calls an async database query function async def fetch_data(). Which is the correct way to write the route?
hard
A. def data(): result = fetch_data(); return result
B. async def data(): result = await fetch_data(); return result
C. async def data(): result = fetch_data(); return result
D. def data(): result = await fetch_data(); return result

Solution

  1. Step 1: Understand async function calls

    To call an async function like fetch_data(), you must use await inside an async function.
  2. Step 2: Match route definition with async call

    The route must be async to use await. async def data(): result = await fetch_data(); return result correctly defines async route and awaits the async call.
  3. Final Answer:

    async def data(): result = await fetch_data(); return result -> Option B
  4. Quick Check:

    Async route + await async call = correct [OK]
Hint: Use async def and await for async calls inside routes [OK]
Common Mistakes:
  • Calling async function without await
  • Using await in sync functions
  • Defining sync route for async calls
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