0
0
FastAPIframework~20 mins

Async generator dependencies in FastAPI - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Async Dependency Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output of this FastAPI async dependency?
Consider this FastAPI async generator dependency. What will the endpoint return when called?
FastAPI
from fastapi import FastAPI, Depends

app = FastAPI()

async def async_dep():
    yield "hello"

@app.get("/test")
async def test(dep: str = Depends(async_dep)):
    return {"message": dep}
A{"message": null}
B{"message": "hello"}
CTypeError: 'async_generator' object is not subscriptable
D500 Internal Server Error
Attempts:
2 left
💡 Hint
Remember that async generator dependencies yield values that are passed to the endpoint parameter.
lifecycle
intermediate
2:00remaining
When is the cleanup code in an async generator dependency executed?
Given this async generator dependency, when will the cleanup code after the yield run?
FastAPI
from fastapi import FastAPI, Depends

app = FastAPI()

async def resource_dep():
    print("Setup resource")
    yield "resource"
    print("Cleanup resource")

@app.get("/use")
async def use_resource(res: str = Depends(resource_dep)):
    return {"used": res}
AOnly if the endpoint raises an exception
BBefore the endpoint function starts executing
CNever, the cleanup code is ignored
DImmediately after the endpoint function finishes executing
Attempts:
2 left
💡 Hint
Think about how async generators work with yield and what happens after yield.
📝 Syntax
advanced
2:00remaining
Which option correctly defines an async generator dependency in FastAPI?
Select the option that correctly defines an async generator dependency that yields a database session.
A
async def get_db():
    db = Session()
    return db
    db.close()
B
def get_db():
    db = Session()
    yield db
    db.close()
C
async def get_db():
    db = Session()
    yield db
    db.close()
D
async def get_db():
    db = Session()
    yield db
    await db.close()
Attempts:
2 left
💡 Hint
Async generator dependencies must use yield and can have cleanup code after yield.
🔧 Debug
advanced
2:00remaining
Why does this async generator dependency cause a runtime error?
Examine the code and select the reason for the runtime error when calling the endpoint.
FastAPI
from fastapi import FastAPI, Depends

app = FastAPI()

async def broken_dep():
    resource = open("file.txt")
    yield resource
    resource.close()

@app.get("/read")
async def read_file(f = Depends(broken_dep)):
    return {"content": f.read()}
AUsing a synchronous file object in an async dependency causes blocking and runtime error
BThe file is closed before reading, causing a ValueError
CThe dependency must be a normal function, not async
DMissing await before resource.read() causes a TypeError
Attempts:
2 left
💡 Hint
Think about mixing synchronous blocking calls inside async functions.
🧠 Conceptual
expert
3:00remaining
What happens if an async generator dependency raises an exception after yield?
In FastAPI, if an async generator dependency yields a value but then raises an exception during cleanup code after yield, what is the effect on the endpoint response?
AThe endpoint response is replaced by a 500 Internal Server Error due to the exception
BThe endpoint response is sent normally, but the exception is logged and does not affect the response
CThe exception is silently ignored and no logs are produced
DThe endpoint never completes and hangs indefinitely
Attempts:
2 left
💡 Hint
Consider how FastAPI handles exceptions in dependency cleanup after the endpoint returns.