diff --git a/changes/2131.doc b/changes/2131.doc new file mode 100644 index 00000000000..0ccfe5135ba --- /dev/null +++ b/changes/2131.doc @@ -0,0 +1 @@ +Add example usage of on_startup and on_shutdown signals by creating and disposing an aiopg connection engine. diff --git a/docs/web.rst b/docs/web.rst index 3f8cc2223bc..0dd6ff83f23 100644 --- a/docs/web.rst +++ b/docs/web.rst @@ -977,6 +977,32 @@ This can be accomplished by subscribing to the app.on_response_prepare.append(on_prepare) +Additionally, the :attr:`~aiohttp.web.Application.on_startup` and +:attr:`~aiohttp.web.Application.on_cleanup` signals can be subscribed to for +application component setup and tear down accordingly. + +The following example will properly initialize and dispose an aiopg connection +engine:: + + from aiopg.sa import create_engine + + async def create_aiopg(app): + app['pg_engine'] = await create_engine( + user='postgre', + database='postgre', + host='localhost', + port=5432, + password='' + ) + + async def dispose_aiopg(app): + app['pg_engine'].close() + await app['pg_engine'].wait_closed() + + app.on_startup.append(create_aiopg) + app.on_cleanup.append(dispose_aiopg) + + Signal handlers should not return a value but may modify incoming mutable parameters.