0

is it not mistake create some connections (to DB, AMQP etc) before run web.run_app in aiohttp. Some example:

async def init_app():
    app = web.Application()

    app['db'] = await create_db_connection()
    app['amqp'] = await create_amqp_connection()

    return app

if __name__ == '__main__':
    app = asyncio.get_event_loop().run_until_complete(init_app())
    web.run_app(app)

It works, but I'm not sure about is this right or not. I know about app.startup but I have I'd like to handle all connection's errors before start main application.

Mike
  • 860
  • 14
  • 24

1 Answers1

1

The code is correct until you don't care about closing resources before the server exit.

Most people don't, that's fine.

Otherwise app.cleanup signal should be used.

Andrew Svetlov
  • 16,730
  • 8
  • 66
  • 69