python連接池pooledDB源碼閱讀參數(shù)的使用
pooledDB參數(shù)詳解
from DBUtils.PooledDB import PooledDB
self.__pool = PooledDB(creator=pymysql, mincached=1, maxcached=4, # 連接池中最大空閑連接數(shù) maxconnections=4,#允許的最大連接數(shù) blocking=True,# 設(shè)置為true,則阻塞并等待直到連接數(shù)量減少,false默認(rèn)情況下將報(bào)告錯(cuò)誤。 ping=1,#默認(rèn)=1表示每當(dāng)從池中獲取時(shí),使用ping()檢查連接 host=self.host, port=self.port, user=self.user, passwd=self.passwd, db=self.db_name, charset=self.charset )
mincached
:連接池中的初始空閑連接,默認(rèn)0或None表示創(chuàng)建連接池時(shí)沒有連接。但對照源碼及實(shí)驗(yàn)效果來看,這個(gè)參數(shù)并沒有起作用。
# PooledDB.py源碼 267行 idle = [self.dedicated_connection() for i in range(mincached)] while idle: idle.pop().close() # 確實(shí)是創(chuàng)建連接池時(shí)創(chuàng)建了mincached個(gè)連接,但返回之前都關(guān)閉了。所以創(chuàng)建好的時(shí)候并沒有mincached個(gè)初始連接
maxcached
:連接池中最大空閑連接數(shù),默認(rèn)0或None表示沒有連接池大小限制maxshared
:最大共享連接數(shù)。默認(rèn)0或None表示所有連接都是專用的maxconnections
:最大允許連接數(shù),默認(rèn)0或None表示沒有連接限制
# PooledDB.py源碼 255行 if maxconnections: if maxconnections < maxcached: maxconnections = maxcached if maxconnections < maxshared: maxconnections = maxshared self._maxconnections = maxconnections else: self._maxconnections = 0 # maxcached、maxshared同時(shí)影響maxconnections # maxconnections=max(maxcached, maxshared)
# PooledDB.py源碼 356行 # 當(dāng)收到一個(gè)連接放回請求時(shí) # if 沒有最大空閑連接數(shù)限制,或現(xiàn)在的空閑連接數(shù)小于最大空閑連接數(shù),則將事務(wù)回滾,并將這個(gè)連接放回空閑連接處; # else:直接關(guān)閉 def cache(self, con): """Put a dedicated專用 connection back into the idle空閑 cache.""" self._lock.acquire() try: if not self._maxcached or len(self._idle_cache) < self._maxcached: con._reset(force=self._reset) # rollback possible transaction # the idle cache is not full, so put it there self._idle_cache.append(con) # append it to the idle cache else: # if the idle cache is already full, con.close() # then close the connection self._connections -= 1 self._lock.notify() finally: self._lock.release() # cache方法被使用 def close(self): """Close the pooled dedicated connection.""" # Instead of actually closing the connection, # return it to the pool for future reuse. if self._con: self._pool.cache(self._con) self._con = None
blocking
:True表示沒有空閑可用連接時(shí),堵塞并等待;False表示直接報(bào)錯(cuò)。默認(rèn)為False。maximum
:單個(gè)連接的最大reuse次數(shù),默認(rèn)0或None表示無限重復(fù)使用,當(dāng)達(dá)到連接大最大使用次數(shù),連接將被重置。
# SteadyDB.py 483行 if self._maxusage: if self._usage >= self._maxusage: # the connection was used too often raise self._failure cursor = self._con.cursor(*args, **kwargs) # try to get a cursor
setsession
: optional list of SQL commands that may serve to prepare the session, 在連接的時(shí)候就會(huì)被執(zhí)行的sql語句。
# SteadyDB.py 298行 def _setsession(self, con=None): """Execute the SQL commands for session preparation.""" if con is None: con = self._con if self._setsession_sql: cursor = con.cursor() for sql in self._setsession_sql: cursor.execute(sql) cursor.close()
reset
:連接放回連接池中時(shí)是如何被重置的,默認(rèn)為True。self._transaction僅在begin()內(nèi)被置為True。默認(rèn)為True時(shí),true的話每次返回連接池都會(huì)回滾事務(wù),F(xiàn)alse的話只會(huì)回滾begin()顯式開啟的事務(wù).
def cache(self, con): """Put a dedicated connection back into the idle cache.""" self._lock.acquire() try: if not self._maxcached or len(self._idle_cache) < self._maxcached: con._reset(force=self._reset) # rollback possible transaction # the idle cache is not full, so put it there self._idle_cache.append(con) # append it to the idle cache else: # if the idle cache is already full, con.close() # then close the connection self._connections -= 1 self._lock.notify() finally: self._lock.release() def _reset(self, force=False): """Reset a tough connection. Rollback if forced or the connection was in a transaction. """ if not self._closed and (force or self._transaction): try: self.rollback() except Exception: pass def begin(self, *args, **kwargs): """Indicate the beginning of a transaction. During a transaction, connections won't be transparently replaced, and all errors will be raised to the application. If the underlying driver supports this method, it will be called with the given parameters (e.g. for distributed transactions). """ self._transaction = True try: begin = self._con.begin except AttributeError: pass else: begin(*args, **kwargs)
failures
:異常類補(bǔ)充,如果(OperationalError, InternalError)這兩個(gè)不夠。
except self._failures as error:
ping: 官方解釋是 (0 = None = never, 1 = default = when _ping_check() is called, 2 = whenever a cursor is created, 4 = when a query is executed, 7 = always, and all other bit combinations of these values 是上面情況的集合),但在源碼中只區(qū)分了是否非零,似乎數(shù)值多少?zèng)]有太大意義。
def _ping_check(self, ping=1, reconnect=True): """Check whether the connection is still alive using ping(). If the the underlying connection is not active and the ping parameter is set accordingly, the connection will be recreated unless the connection is currently inside a transaction. """ if ping & self._ping: try: # if possible, ping the connection alive = self._con.ping() except (AttributeError, IndexError, TypeError, ValueError): self._ping = 0 # ping() is not available alive = None reconnect = False except Exception: alive = False else: if alive is None: alive = True if alive: reconnect = False if reconnect and not self._transaction: try: # try to reopen the connection con = self._create() except Exception: pass else: self._close() self._store(con) alive = True return alive
使用方法
def start_conn(self): try: # maxshared 允許的最大共享連接數(shù),默認(rèn)0/None表示所有連接都是專用的 # 當(dāng)線程關(guān)閉不再共享的連接時(shí),它將返回到空閑連接池中,以便可以再次對其進(jìn)行回收。 # mincached 連接池中空閑連接的初始連接數(shù),實(shí)驗(yàn)證明沒啥用 self.__pool = PooledDB(creator=pymysql, mincached=1, # mincached 連接池中空閑連接的初始連接數(shù),但其實(shí)沒用 maxcached=4, # 連接池中最大空閑連接數(shù) maxshared=3, #允許的最大共享連接數(shù) maxconnections=2, # 允許的最大連接數(shù) blocking=False, # 設(shè)置為true,則阻塞并等待直到連接數(shù)量減少,false默認(rèn)情況下將報(bào)告錯(cuò)誤。 host=self.host, port=self.port, user=self.user, passwd=self.passwd, db=self.db_name, charset=self.charset ) print("0 start_conn連接數(shù):%s " % (self.__pool._connections)) self.conn = self.__pool.connection() print('connect success') print("1 start_conn連接數(shù):%s " % (self.__pool._connections)) self.conn2 = self.__pool.connection() print("2 start_conn連接數(shù):%s " % (self.__pool._connections)) db3 = self.__pool.connection() print("3 start_conn連接數(shù):%s " % (self.__pool._connections)) db4 = self.__pool.connection() print("4 start_conn連接數(shù):%s " % (self.__pool._connections)) db5 = self.__pool.connection() print("5 start_conn連接數(shù):%s " % (self.__pool._connections)) # self.conn.close() print("6 start_conn連接數(shù):%s " % (self.__pool._connections)) return True except: print('connect failed') return False
0 start_conn連接數(shù):0
connect success
1 start_conn連接數(shù):1
2 start_conn連接數(shù):2
3 start_conn連接數(shù):3
4 start_conn連接數(shù):4
connect failed
如上程序,可對照試驗(yàn)結(jié)果,詳細(xì)理解一下上述的幾個(gè)參數(shù)。
- mincached確實(shí)沒用,pooledDB對象生成退出后,并沒有mincached個(gè)初始化連接。
- maxconnections = max(maxcached,maxshared),對照結(jié)果來看,最大的連接數(shù)顯然等于maxcached,maxshared的較大者4,所以可以連續(xù)開四個(gè)連接,但到第5個(gè)時(shí)顯示連接失敗。
- 若將blocking改為True,則實(shí)驗(yàn)結(jié)果最后一行的”connect failed“不會(huì)出現(xiàn),程序會(huì)一直堵塞等待新的空閑連接出現(xiàn),在本例中,沒有操作關(guān)閉原有連接,程序會(huì)一直堵塞等待。
參考資料:
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
使用wxpython實(shí)現(xiàn)的一個(gè)簡單圖片瀏覽器實(shí)例
這篇文章主要介紹了使用wxpython實(shí)現(xiàn)的一個(gè)簡單圖片瀏覽器實(shí)例,根據(jù)自己的特殊需求而寫,需要的朋友可以參考下2014-07-07使用python畫社交網(wǎng)絡(luò)圖實(shí)例代碼
這篇文章主要給大家介紹了關(guān)于如何使用python畫社交網(wǎng)絡(luò)圖的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用python具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07Python之time模塊的時(shí)間戳,時(shí)間字符串格式化與轉(zhuǎn)換方法(13位時(shí)間戳)
今天小編就為大家分享一篇Python之time模塊的時(shí)間戳,時(shí)間字符串格式化與轉(zhuǎn)換方法(13位時(shí)間戳),具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-08-08Python實(shí)現(xiàn)圖片格式轉(zhuǎn)換小程序
這篇文章主要為大家詳細(xì)介紹了Python實(shí)現(xiàn)圖片格式轉(zhuǎn)換小程序,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-08-08linux環(huán)境下安裝python虛擬環(huán)境及注意事項(xiàng)
這篇文章主要介紹了linux環(huán)境下安裝python虛擬環(huán)境,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-01-01Python利用capstone實(shí)現(xiàn)反匯編
Capstone是一個(gè)輕量級(jí)的多平臺(tái)、多架構(gòu)的反匯編框架,該模塊支持目前所有通用操作系統(tǒng),反匯編架構(gòu)幾乎全部支持。本文就將利用他實(shí)現(xiàn)反匯編,感興趣的可以了解下2022-04-04Python自定義sorted排序?qū)崿F(xiàn)方法詳解
這篇文章主要介紹了Python自定義sorted排序?qū)崿F(xiàn)方法詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-09-09python性能測試手機(jī)號(hào)驗(yàn)證碼登錄壓測示例詳解
這篇文章主要為大家介紹了python性能測試手機(jī)號(hào)驗(yàn)證碼登錄壓測的示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-07-07