Skip to content

Commit

Permalink
Use cleanup_ctx in pool usage doc (#878)
Browse files Browse the repository at this point in the history
Co-authored-by: Elvis Pranskevichus <elvis@edgedb.com>
  • Loading branch information
ir4y and elprans authored Oct 9, 2023
1 parent ccc7baf commit 70c8bd8
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions docs/usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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.

0 comments on commit 70c8bd8

Please sign in to comment.