在 运行 aiohttp 循环之前创建数据库连接
Create db connections before run aiohttp loop
在 aiohttp 中 运行 web.run_app
之前创建一些连接(到 DB、AMQP 等)是不是错误。
一些例子:
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)
它有效,但我不确定这是否正确。
我知道 app.startup
但我想在启动主应用程序之前处理所有连接错误。
除非您不关心在服务器退出前关闭资源,否则代码是正确的。
大多数人不知道,没关系。
否则应使用app.cleanup
信号。
在 aiohttp 中 运行 web.run_app
之前创建一些连接(到 DB、AMQP 等)是不是错误。
一些例子:
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)
它有效,但我不确定这是否正确。
我知道 app.startup
但我想在启动主应用程序之前处理所有连接错误。
除非您不关心在服务器退出前关闭资源,否则代码是正确的。
大多数人不知道,没关系。
否则应使用app.cleanup
信号。