0
0
FastAPIframework~10 mins

Async path operations in FastAPI - Interactive Code Practice

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

Complete the code to define an async GET path operation in FastAPI.

FastAPI
from fastapi import FastAPI

app = FastAPI()

@app.get("/items/{item_id}")
async def read_item(item_id: int):
    return {"item_id": item_id, "message": "This is an async path"}

# The function must be [1]
Drag options to blanks, or click blank then click option'
Async
Bdef
Clambda
Dasync
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'def' instead of 'async def' for async path operations.
Trying to use 'sync' which is not a Python keyword.
2fill in blank
medium

Complete the code to await an async function inside a FastAPI path operation.

FastAPI
from fastapi import FastAPI
import asyncio

app = FastAPI()

async def fetch_data():
    await asyncio.sleep(1)
    return "data"

@app.get("/data")
async def get_data():
    result = [1] fetch_data()
    return {"result": result}
Drag options to blanks, or click blank then click option'
Afetch_data()
Bawait
Casync
Dyield
Attempts:
3 left
💡 Hint
Common Mistakes
Calling the async function without 'await' causing a coroutine object to be returned.
Using 'async' instead of 'await' inside the function body.
3fill in blank
hard

Fix the error in the async path operation by completing the code.

FastAPI
from fastapi import FastAPI

app = FastAPI()

@app.get("/users/{user_id}")
def get_user(user_id: int):
    return {"user_id": user_id}

# To make this async, add [1] before def
Drag options to blanks, or click blank then click option'
Aasync
Bawait
Casync def
Ddef async
Attempts:
3 left
💡 Hint
Common Mistakes
Writing 'async def' as one token in the blank instead of just 'async'.
Placing 'async' after 'def' which is invalid syntax.
4fill in blank
hard

Fill both blanks to create an async POST path operation that receives JSON data and returns it.

FastAPI
from fastapi import FastAPI
from pydantic import BaseModel

app = FastAPI()

class Item(BaseModel):
    name: str
    price: float

@app.post("/items")
async def create_item(item: [1]):
    return [2]
Drag options to blanks, or click blank then click option'
AItem
Bitem.dict()
Cdict()
DItem()
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'dict()' instead of 'item.dict()' which returns an empty dict.
Returning the model instance directly without converting to dict.
5fill in blank
hard

Fill all three blanks to define an async path operation that waits for a background task and returns a message.

FastAPI
from fastapi import FastAPI
import asyncio

app = FastAPI()

async def background_task():
    await asyncio.sleep(2)

@app.get("/wait")
async def wait_task():
    await [1]()
    return [2]
Drag options to blanks, or click blank then click option'
Abackground_task
B{"message": "Task completed"}
Cbackground_task()
D"Task completed"
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'background_task()' inside await which calls the function twice.
Returning a plain string instead of a dictionary for JSON response.