Skip to content

Instantly share code, notes, and snippets.

@notpushkin
Created October 4, 2023 11:59
Show Gist options
  • Save notpushkin/cfa23e4ab8a73ef9505e2b6e6b2dca5c to your computer and use it in GitHub Desktop.
Save notpushkin/cfa23e4ab8a73ef9505e2b6e6b2dca5c to your computer and use it in GitHub Desktop.
from fastapi import FastAPI, Depends
from contextlib import asynccontextmanager, contextmanager
app = FastAPI()
@contextmanager
def get_answer():
yield 42
print("cleanup get_answer")
@asynccontextmanager
async def get_async_answer():
yield 43
print("cleanup get_async_answer")
@app.get("/")
def route_get_answer(
answer: int = Depends(get_answer),
async_answer: int = Depends(get_async_answer),
):
return {
"answer": answer,
"async_answer": async_answer,
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment