Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update example of pool usage with aiohttp #878

Merged
merged 2 commits into from
Oct 9, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions docs/usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -399,20 +399,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.