에듀테크 프로젝트 대시보드
Lifespan 이벤트
앱 시작 시 커넥션 풀·ML 모델·HTTP 클라이언트를 만들고, 종료 시 정리합니다.
from contextlib import asynccontextmanager
from fastapi import FastAPI
@asynccontextmanager
async def lifespan(app: FastAPI):
# ── startup ──
app.state.http = httpx.AsyncClient()
app.state.db_pool = await create_pool(settings.database_url)
yield
# ── shutdown ──
await app.state.http.aclose()
await app.state.db_pool.close()
app = FastAPI(lifespan=lifespan)from fastapi import Request
@app.get("/proxy")
async def proxy(request: Request):
r = await request.app.state.http.get("https://api.example.com")
return r.json()def get_http(request: Request) -> httpx.AsyncClient:
return request.app.state.http
HttpDep = Annotated[httpx.AsyncClient, Depends(get_http)]
@app.get("/proxy")
async def proxy(http: HttpDep):
r = await http.get("https://api.example.com")
return r.json()# deprecated — 새 코드는 lifespan 사용
@app.on_event("startup")
async def startup(): ...
@app.on_event("shutdown")
async def shutdown(): ...pool_size 설정 시 워커수 × pool_size 가 실제 커넥션 상한 — PostgreSQL max_connections 와 비교