From 3b45493e03ccf28df1f95a42f34b9b0a359564fb Mon Sep 17 00:00:00 2001 From: wuaho Date: Tue, 27 Jul 2021 16:26:31 +0800 Subject: [PATCH] =?UTF-8?q?ck=20=E8=BF=9E=E6=8E=A5=E6=B1=A0=E4=BC=98?= =?UTF-8?q?=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- db/ckdb.py | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) 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)