Python 使用aiomysql在aiohttp中创建连接池
我需要能够像这样访问aiohttp应用程序中的aiomysql连接池Python 使用aiomysql在aiohttp中创建连接池,python,python-asyncio,aiohttp,aio-mysql,Python,Python Asyncio,Aiohttp,Aio Mysql,我需要能够像这样访问aiohttp应用程序中的aiomysql连接池 async def get(请求): 与request.app['db'].acquire()作为conn异步: 将connection.cursor()作为cur进行异步: 等待当前执行('SELECT…')) rows=wait cur.fetchall() 返回web.json_响应(行) 要在应用程序中保留连接池,我知道我需要执行以下操作 app=web.Application() loop=asyncio.get\
async def get(请求):
与request.app['db'].acquire()作为conn异步:
将connection.cursor()作为cur进行异步:
等待当前执行('SELECT…'))
rows=wait cur.fetchall()
返回web.json_响应(行)
要在应用程序中保留连接池,我知道我需要执行以下操作
app=web.Application()
loop=asyncio.get\u event\u loop()
app['db']=aimysql.create_池(db='…',user='…',password='…',loop=loop)
应用程序。添加_路线(路线)
web.run_应用程序(应用程序)
但是,这显然是失败的,因为aimysql.create_池是一个协程。这里的正确语法是什么?我知道已经很晚了,但希望它能帮助一些人, 您可以通过以下方式创建池
import aiomysql
async def init_db(app):
pool = await aiomysql.create_pool(host='host', port=2222),
user='user', password='password',
db='dbname')
app['mysql_db'] = pool
然后您可以使用池来获取连接
async def someApi(request):
pool = request.app["mysql_db"]
async with pool.acquire() as conn:
async with conn.cursor() as cur:
await cur.execute("select * from table")
rows = await cur.fetchall()
你应该考虑it@ArtemiyRodionov好极了<代码>应用程序。启动时。追加是我需要的。