From 70c8bd814f9d289741845fa8dc761737f7ea4e78 Mon Sep 17 00:00:00 2001 From: Ilya Beda Date: Tue, 10 Oct 2023 04:44:33 +1100 Subject: [PATCH] Use cleanup_ctx in pool usage doc (#878) Co-authored-by: Elvis Pranskevichus --- docs/usage.rst | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/docs/usage.rst b/docs/usage.rst index 91897790..21281b6b 100644 --- a/docs/usage.rst +++ b/docs/usage.rst @@ -438,20 +438,26 @@ Web service that computes the requested power of two. text="2 ^ {} is {}".format(power, result)) - async def init_app(): + async def init_db(app): + """Initialize a connection pool.""" + app['pool'] = await asyncpg.create_pool(database='postgres', + user='postgres') + yield + app['pool'].close() + + + def init_app(): """Initialize the application server.""" app = web.Application() - # Create a database connection pool - app['pool'] = await asyncpg.create_pool(database='postgres', - user='postgres') + # Create a database context + app.cleanup_ctx.append(init_db) # Configure service routes app.router.add_route('GET', '/{power:\d+}', handle) app.router.add_route('GET', '/', handle) return app - loop = asyncio.get_event_loop() - app = loop.run_until_complete(init_app()) + app = init_app() web.run_app(app) See :ref:`asyncpg-api-pool` API documentation for more information.