Python 3.8, aiomysql
I have a code:
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()
But, instead of working, I have an runtime error
async with self.pool.acquire() as con:
AttributeError: '_PoolContextManager' object has no attribute 'acquire'
For some reason, aiomysql.create_pool()
returns _PoolContextManager
and not Pool
instance.
Same thing with aiomysql.connect()
- it returns _ConnectionContextManager
, instead of Connection
What I should do to get instance from creat_pool()
or connect()
or how I should deal with ContextManager
.
Maby, i do something completely wrong.