Skip to content

Commit 875c0f2

Browse files
committed
try/finally in readme ex.
1 parent bf48570 commit 875c0f2

1 file changed

Lines changed: 5 additions & 2 deletions

File tree

README.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,11 @@ async def app_setup_teardown(_: FastAPI) -> typing.AsyncGenerator[None, None]:
3838
conn = await asyncpg.connect()
3939
# Connect the listener to the database using the specified channel.
4040
await listener.connect(conn)
41-
yield # Yield control back to the event loop.
42-
await conn.close() # Ensure the database connection is closed on app teardown.
41+
42+
try:
43+
yield
44+
finally:
45+
await conn.close() # Ensure the database connection is closed on app teardown.
4346

4447
# Create an instance of FastAPI, specifying the app setup and teardown actions.
4548
APP = FastAPI(lifespan=app_setup_teardown)

0 commit comments

Comments
 (0)