diff --git a/db/ckdb.py b/db/ckdb.py index 397f111..734bae3 100644 --- a/db/ckdb.py +++ b/db/ckdb.py @@ -1,3 +1,4 @@ +import asyncio import datetime from aioch import Client @@ -8,11 +9,24 @@ class CKDrive: ClientPool = set() @classmethod - async def _execute(cls, *args, **kwargs): - client = cls.ClientPool.pop() - res = await client.execute(*args, **kwargs) - CKDrive.ClientPool.add(client) - return res + async def _execute(cls, *args, typ_cnt=5, **kwargs): + if not cls.ClientPool: + if typ_cnt < 0: + raise Exception('连接池耗尽') + + await asyncio.sleep(1) + await cls._execute(*args, **kwargs, typ_cnt=typ_cnt - 1) + client = None + try: + client = cls.ClientPool.pop() + res = await client.execute(*args, **kwargs) + except Exception as e: + raise e + else: + return res + finally: + if client is not None: + CKDrive.ClientPool.add(client) async def execute(self, sql) -> dict: data, columns = await self._execute(sql, with_column_types=True, columnar=True)