0
0
FastAPIframework~10 mins

Why async improves performance in FastAPI - Test Your Understanding

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to define an async route handler in FastAPI.

FastAPI
from fastapi import FastAPI

app = FastAPI()

@app.get("/items")
async def get_items():
    return {"message": "Hello from [1] function!"}
Drag options to blanks, or click blank then click option'
Async
Basync
Cnormal
Dblocking
Attempts:
3 left
💡 Hint
Common Mistakes
Using a normal def function instead of async def
Forgetting to add async keyword before function
2fill in blank
medium

Complete the code to await an async operation inside the route handler.

FastAPI
import asyncio
from fastapi import FastAPI

app = FastAPI()

@app.get("/wait")
async def wait_seconds():
    await [1](1)
    return {"message": "Waited 1 second"}
Drag options to blanks, or click blank then click option'
Aasyncio.sleep
Btime.sleep
Csleep
Dwait
Attempts:
3 left
💡 Hint
Common Mistakes
Using time.sleep which blocks the event loop
Not using await before the sleep call
3fill in blank
hard

Fix the syntax error in the route handler by completing the code correctly.

FastAPI
from fastapi import FastAPI

app = FastAPI()

@app.get("/data")
def [1]():
    return {"data": "sync function"}
Drag options to blanks, or click blank then click option'
Aget_data
Basync def get_data
Casync get_data
Dasync def get_data()
Attempts:
3 left
💡 Hint
Common Mistakes
Putting async def inside the blank which causes syntax error
Adding parentheses in the blank
4fill in blank
hard

Fill both blanks to create a dictionary comprehension that filters async tasks with duration greater than 2.

FastAPI
tasks = {task: duration for task, duration in durations.items() if duration [1] 2 and task.startswith([2])}
Drag options to blanks, or click blank then click option'
A>
B"a"
C<
D"b"
Attempts:
3 left
💡 Hint
Common Mistakes
Using less than symbol which filters wrong tasks
Using wrong starting letter string
5fill in blank
hard

Fill all three blanks to create a dictionary comprehension that maps uppercase task names to durations for tasks longer than 3 seconds.

FastAPI
result = [1]: [2] for [3], duration in durations.items() if duration > 3
Drag options to blanks, or click blank then click option'
Atask.upper()
Bduration
Ctask
Dduration.upper()
Attempts:
3 left
💡 Hint
Common Mistakes
Using duration.upper() which is invalid for numbers
Swapping key and value positions