When I call functions an error occurs
RuntimeError: Cannot acquire connection after closing pool
but I don’t close the pool anywhere
from aiomysql import create_pool, Cursor
import asyncio
from env import DATABASE, HOST, PORT, USER, PASSWORD
class Table:
def __init__(self) -> None:
l = asyncio.get_event_loop()
self.pool = l.run_until_complete(create_pool(
host=HOST,
port=PORT,
user=USER,
password=PASSWORD,
db=DATABASE,
cursorclass=Cursor,
autocommit=True,
loop = asyncio.get_event_loop(),
maxsize=15
))
async def f(self):
async with self.pool as pool:
async with pool.acquire() as conn:
async with conn.cursor() as cursor:
QUERY = f"""SELECT 1;"""
await cursor.execute(QUERY, (user_id))
return (await cursor.fetchone())[0]
Traceback:
File "C:UsersUserAppDataLocalProgramsPythonPython311Libsite-packagesaiogramdispatcherdispatcher.py", line 415, in _process_polling_updates
for responses in itertools.chain.from_iterable(await self.process_updates(updates, fast)):
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:UsersUserAppDataLocalProgramsPythonPython311Libsite-packagesaiogramdispatcherdispatcher.py", line 235, in process_updates
return await asyncio.gather(*tasks)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:UsersUserAppDataLocalProgramsPythonPython311Libsite-packagesaiogramdispatcherhandler.py", line 117, in notify
response = await handler_obj.handler(*args, **partial_data)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:UsersUserAppDataLocalProgramsPythonPython311Libsite-packagesaiogramdispatcherdispatcher.py", line 256, in process_update
return await self.message_handlers.notify(update.message)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:UsersUserAppDataLocalProgramsPythonPython311Libsite-packagesaiogramdispatcherhandler.py", line 117, in notify
response = await handler_obj.handler(*args, **partial_data)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "c:UsersUserDesktopmain.py", line 137, in start
test = await table.f()
^^^^^^^^^^^^^^^
File "c:UsersUserDesktopmain.py", line 43, in f
async with pool.acquire() as conn:
File "C:UsersUserAppDataLocalProgramsPythonPython311Libsite-packagesaiomysqlutils.py", line 134, in __aenter__
self._conn = await self._coro
^^^^^^^^^^^^^^^^
File "C:UsersUserAppDataLocalProgramsPythonPython311Libsite-packagesaiomysqlpool.py", line 139, in _acquire
raise RuntimeError("Cannot acquire connection after closing pool")
RuntimeError: Cannot acquire connection after closing pool
New contributor
izen02 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
6