This commit is contained in:
yumoqing 2019-05-15 17:53:36 +08:00
parent ffa87bfd59
commit 12be14d8c3
3 changed files with 8 additions and 11 deletions

Binary file not shown.

View File

@ -1,6 +1,5 @@
import asyncio import asyncio
from queue import Queue
from functools import wraps from functools import wraps
import codecs import codecs
@ -104,21 +103,20 @@ class ConnectionPool(object):
self.driver = myImport(self.dbdesc['driver']) self.driver = myImport(self.dbdesc['driver'])
self.maxconn = dbdesc.get('maxconn',5) self.maxconn = dbdesc.get('maxconn',5)
self.maxuse = dbdesc.get('maxuse',1000) self.maxuse = dbdesc.get('maxuse',1000)
self._pool = Queue(self.maxconn) self._pool = asyncio.Queue(self.maxconn)
self._fillPool()
self.using = [] self.using = []
self.use_cnt = 0 self.use_cnt = 0
self.max_use = 1000 self.max_use = 1000
def _fillPool(self): async def _fillPool(self):
for i in range(self.maxconn): for i in range(self.maxconn):
lc = self.connect() lc = await self.connect()
i = i + 1 i = i + 1
def connect(self): async def connect(self):
lc = LifeConnect(self.driver.connect,self.dbdesc['kwargs'], lc = LifeConnect(self.driver.connect,self.dbdesc['kwargs'],
use_max=self.maxuse,async_mode=self.async_mode) use_max=self.maxuse,async_mode=self.async_mode)
self._pool.put(lc) await self._pool.put(lc)
return lc return lc
def isEmpty(self): def isEmpty(self):
@ -128,7 +126,7 @@ class ConnectionPool(object):
return self._pool.full() return self._pool.full()
async def aquire(self): async def aquire(self):
lc = self._pool.get() lc = await self._pool.get()
self.using.append(lc) self.using.append(lc)
conn = await lc.use() conn = await lc.use()
return conn return conn
@ -136,7 +134,7 @@ class ConnectionPool(object):
async def release(self,conn): async def release(self,conn):
lc = await LifeConnect.free(conn) lc = await LifeConnect.free(conn)
self.using = [c for c in self.using if c != lc ] self.using = [c for c in self.using if c != lc ]
self._pool.put(lc) await self._pool.put(lc)
@SingletonDecorator @SingletonDecorator
class DBPools: class DBPools:
@ -165,6 +163,7 @@ class DBPools:
p = self._cpools.get(dbname) p = self._cpools.get(dbname)
if p == None: if p == None:
p = ConnectionPool(self.databases.get(dbname),self.loop) p = ConnectionPool(self.databases.get(dbname),self.loop)
await p._fillPool()
self._cpools[dbname] = p self._cpools[dbname] = p
conn = await p.aquire() conn = await p.aquire()
if self.isAsyncDriver(dbname): if self.isAsyncDriver(dbname):
@ -233,7 +232,6 @@ class DBPools:
"total":total, "total":total,
"rows":recs "rows":recs
} }
print(len(recs),'records return')
return data return data
except Exception as e: except Exception as e:
print('error',e) print('error',e)

View File

@ -310,7 +310,6 @@ class SQLor(object):
c = Cnt() c = Cnt()
await self.runSQL(cnt_desc,NS,c.handler) await self.runSQL(cnt_desc,NS,c.handler)
print(c.recs[0])
t = c.recs[0]['rcnt'] t = c.recs[0]['rcnt']
return t return t