Consider a FastAPI dependency function that uses yield to provide a database connection. What is the behavior of this dependency during a request?
from fastapi import Depends, FastAPI from contextlib import asynccontextmanager app = FastAPI() @asynccontextmanager def get_db(): db = "db connection" try: yield db finally: print("Closing db connection") @app.get("/items/") async def read_items(db=Depends(get_db)): return {"db": db}
Think about how yield works in Python generators and how FastAPI uses it for dependencies.
FastAPI treats dependencies with yield as context managers. It runs code before yield when the request starts, and runs the code after yield when the request ends, allowing cleanup like closing connections.
Given this FastAPI dependency that yields a resource and then raises an exception in the cleanup code, what happens when the endpoint is called?
from fastapi import Depends, FastAPI app = FastAPI() def resource(): print("Setup resource") yield "resource" print("Cleanup resource") raise RuntimeError("Error during cleanup") @app.get("/test") async def test(dep=Depends(resource)): return {"message": "success"}
Consider when the cleanup code after yield runs in FastAPI dependencies.
The code before yield runs before the request, the yield provides the value, and the code after yield runs after the response is sent. Exceptions in cleanup run after response, so the client still gets success.
Examine this FastAPI dependency that tries to open and close a connection. Why does the connection never close?
from fastapi import Depends, FastAPI app = FastAPI() def get_conn(): conn = "open connection" return conn print("Closing connection") @app.get("/data") async def data(conn=Depends(get_conn)): return {"conn": conn}
Think about what happens to code after a return statement in Python functions.
Code after a return statement is never executed. So the print statement that should close the connection never runs, leaving the connection open.
FastAPI allows defining lifespan events to manage resources like database connections. Which statement best describes how lifespan events work?
Think about app startup and shutdown phases versus request handling.
Lifespan events run once at app startup and once at shutdown, ideal for managing resources shared across requests like database pools.
Given two nested dependencies where the outer depends on the inner, both using yield to manage connections, what is the order of opening and closing connections during a request?
from fastapi import Depends, FastAPI app = FastAPI() def inner_conn(): print("Open inner connection") yield "inner" print("Close inner connection") def outer_conn(inner=Depends(inner_conn)): print("Open outer connection") yield f"outer uses {inner}" print("Close outer connection") @app.get("/nested") async def nested(dep=Depends(outer_conn)): return {"connection": dep}
Consider how Python generators and context managers nest when using yield in dependencies.
The inner dependency opens first, then the outer. On cleanup, the outer closes first, then the inner, following stack unwinding order.