This commit is contained in:
2024-12-30 18:17:33 +05:30
parent de46a55353
commit fcc3d4b2e8
10 changed files with 6626 additions and 5765 deletions
+20 -9
View File
@@ -112,7 +112,10 @@ class AsyncMySQL:
self.__kwargs = kwargs
self.__min_pool_size = 10
self.__max_pool_size = max(pool_size, self.__min_pool_size)
self.__pool = None
self.__pool: aiomysql = None
# For establishing connection:
self.__exclusive_semaphore = asyncio.Semaphore(1)
# Minor adjustments for backward compatibility:
self.__kwargs["db"] = self.__kwargs.pop("database")
@@ -123,26 +126,34 @@ class AsyncMySQL:
def __del__(self):
pass
async def connect(self):
async def connect(self) -> bool:
"""
Establish a connection and create a pool of connections to call from.
:return: None.
:return: True if connected, else False
"""
try:
self.__pool = await aiomysql.create_pool(
minsize = self.__min_pool_size,
maxsize = self.__max_pool_size,
loop = asyncio.get_event_loop(),
**self.__kwargs
)
# Ensure that only one connection attempt is being made at one time,
# and try to connect to the database:
async with self.__exclusive_semaphore:
if self.__pool is None:
self.__pool = await aiomysql.create_pool(
minsize = self.__min_pool_size,
maxsize = self.__max_pool_size,
loop = asyncio.get_event_loop(),
**self.__kwargs
)
# In case of any exception:
except Exception as exception:
self.__printer(exception)
self.__pool = None
# Done here:
return False if self.__pool is None else True
async def ensure_connection(self):
"""