Python 3.8,aiomysql
我有一个代码:
import aiomysql
class AsyncSQL:
def __init__(self, loop):
self.pool = aiomysql.create_pool(host="localhost", user="root", password="root",
db="chatdb", charset="utf8", loop=loop)
async def select_query(self, query):
async with self.pool.acquire() as con: # HERE ERROR APPEARS
async with con.cursor() as cur:
await cur.execute(query)
rows = await cur.fetchall()
desc = await cur.description()
await cur.close()
return rows, desc
async def insert_query(self, query):
async with self.pool.acquire() as con: # HERE ERROR APPEARS
async with con.cursor() as cur:
await cur.execute(query)
await con.commit()
await cur.close()
async def update_query(self, query):
async with self.pool.acquire() as con: # HERE ERROR APPEARS
async with con.cursor() as cur:
await cur.execute(query)
await con.commit()
rows = await cur.fetchall()
desc = await cur.description()
await cur.close()
return rows, desc
def close_connection(self):
self.pool.close()
但是,我没有工作,而是遇到了运行时错误
async with self.pool.acquire() as con:
AttributeError: '_PoolContextManager' object has no attribute 'acquire'
由于某种原因,aiomysql.create_pool()
返回_PoolContextManager
而不是Pool
实例。
同样的事情aiomysql.connect()
- 它返回_ConnectionContextManager
,而不是Connection
我应该做些什么来获取实例,creat_pool()
或者connect()
我应该如何处理ContextManager
. 梅比,我做错了什么。