ck 连接池优化
This commit is contained in:
parent
adf440f977
commit
3b45493e03
24
db/ckdb.py
24
db/ckdb.py
@ -1,3 +1,4 @@
|
|||||||
|
import asyncio
|
||||||
import datetime
|
import datetime
|
||||||
|
|
||||||
from aioch import Client
|
from aioch import Client
|
||||||
@ -8,11 +9,24 @@ class CKDrive:
|
|||||||
ClientPool = set()
|
ClientPool = set()
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
async def _execute(cls, *args, **kwargs):
|
async def _execute(cls, *args, typ_cnt=5, **kwargs):
|
||||||
client = cls.ClientPool.pop()
|
if not cls.ClientPool:
|
||||||
res = await client.execute(*args, **kwargs)
|
if typ_cnt < 0:
|
||||||
CKDrive.ClientPool.add(client)
|
raise Exception('连接池耗尽')
|
||||||
return res
|
|
||||||
|
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:
|
async def execute(self, sql) -> dict:
|
||||||
data, columns = await self._execute(sql, with_column_types=True, columnar=True)
|
data, columns = await self._execute(sql, with_column_types=True, columnar=True)
|
||||||
|
Loading…
Reference in New Issue
Block a user