You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

24 lines
579 B

from contextlib import asynccontextmanager
from fastapi import Depends, FastAPI, Request
@asynccontextmanager
async def lifespan(app: FastAPI):
# Initialize a shared state when the app starts
app.state.some_state = "some_state_open"
yield
# Cleanup the shared state when the app shuts down
app.state.some_state = "some_state_close"
async def get_some_state(request: Request):
return request.app.state.some_state
app = FastAPI(lifespan=lifespan)
@app.get("/")
async def root(some_state=Depends(get_some_state)):
return {"message": some_state}