]> git.ipfire.org Git - thirdparty/psycopg.git/commitdiff
docs: replace fastapi events with lifespan
authorVinz <vinzenz.klass@ba.valantic.com>
Sat, 14 Sep 2024 13:31:04 +0000 (15:31 +0200)
committerDaniele Varrazzo <daniele.varrazzo@gmail.com>
Sat, 14 Sep 2024 14:42:16 +0000 (16:42 +0200)
docs/advanced/pool.rst

index 74fa4e3d29e3dd0d41ecc75d75d151fb0635bab7..46fc308e7ce04e1edc277fa0d3c881b128359f3c 100644 (file)
@@ -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