【发布时间】:2020-10-18 16:24:36
【问题描述】:
我需要能够像这样访问 aiohttp 应用程序中的 aiomysql 连接池
async def get(request):
async with request.app['db'].acquire() as conn:
async with connection.cursor() as cur:
await cur.execute('SELECT ...')
rows = await cur.fetchall()
return web.json_response(rows)
为了将连接池保留在应用程序中,我知道我需要执行类似的操作
app = web.Application()
loop = asyncio.get_event_loop()
app['db'] = aiomysql.create_pool(db='...', user='...', password='...', loop=loop)
app.add_routes(routes)
web.run_app(app)
但是,这显然失败了,因为 aiomysql.create_pool 是一个协程。这里的正确语法是什么?
【问题讨论】:
-
你应该考虑aiohttp signals
-
@ArtemiyRodionov 非常棒。
app.on_startup.append是我需要的。
标签: python python-asyncio aiohttp aio-mysql