From: Vinz Date: Sat, 14 Sep 2024 13:31:04 +0000 (+0200) Subject: docs: replace fastapi events with lifespan X-Git-Tag: 3.2.2~2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=a5b67ba942c418d4e306c121c1bf827884e9c36d;p=thirdparty%2Fpsycopg.git docs: replace fastapi events with lifespan --- diff --git a/docs/advanced/pool.rst b/docs/advanced/pool.rst index 74fa4e3d2..46fc308e7 100644 --- a/docs/advanced/pool.rst +++ b/docs/advanced/pool.rst @@ -160,19 +160,19 @@ import time. If that's too early, and you want to delay opening connections until the application is ready, you can specify to create a closed pool and call the `~ConnectionPool.open()` method (and optionally the `~ClonnectionPool.close()` method) at application startup/shutdown. For -example, in FastAPI, you can use `startup/shutdown events`__:: +example, in FastAPI, you can use a `Lifespan`__:: pool = AsyncConnectionPool(..., open=False, ...) - @app.on_event("startup") - async def open_pool(): + @asynccontextmanager + async def lifespan(instance: FastAPI): await pool.open() - - @app.on_event("shutdown") - async def close_pool(): + yield await pool.close() -.. __: https://fastapi.tiangolo.com/advanced/events/#events-startup-shutdown + app = FastAPI(lifespan=lifespan) + +.. __: https://fastapi.tiangolo.com/advanced/events/#lifespan .. warning:: The current default for the `!open` parameter is `!True`. However this