ck 连接池优化
This commit is contained in:
parent
adf440f977
commit
3b45493e03
18
db/ckdb.py
18
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):
|
||||
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)
|
||||
CKDrive.ClientPool.add(client)
|
||||
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)
|
||||
|
Loading…
Reference in New Issue
Block a user