python數據庫連接池DBUtils.PooledDB


DBUtils 是一套用於管理數據庫連接池的包,爲高頻度高併發的數據庫訪問提供更好的性能,可以自動管理連接對象的創建和釋放。最常用的兩個外部接口是 PersistentDB 和 PooledDB,前者提供了單個線程專用的數據庫連接池,後者則是進程內所有線程共享的數據庫連接池。

簡介
DBUtils是一套Python數據庫連接池包,並允許對非線程安全的數據庫接口進行線程安全包裝。DBUtils來自Webware for Python。

DBUtils提供兩種外部接口:

PersistentDB :提供線程專用的數據庫連接,並自動管理連接。
PooledDB :提供線程間可共享的數據庫連接,並自動管理連接。
實測證明 PersistentDB 的速度是最高的,但是在某些特殊情況下,數據庫的連接過程可能異常緩慢,而此時的PooledDB則可以提供相對來說平均連接時間比較短的管理方式。

另外,實際使用的數據庫驅動也有所依賴,比如SQLite數據庫只能使用PersistentDB作連接池。 下載地址:http://www.webwareforpython.org/downloads/DBUtils/

使用方法
連接池對象只初始化一次,一般可以作爲模塊級代碼來確保。 PersistentDB的連接例子:

import DBUtils.PersistentDB 
persist=DBUtils.PersistentDB.PersistentDB(dbpai=MySQLdb,maxusage=1000,**kwargs)



這裏的參數dbpai指使用的底層數據庫模塊,兼容DB-API的。maxusage則爲一個連接最大使用次數,參考了官方例子。後面的**kwargs則爲實際傳遞給MySQLdb的參數。

獲取連接: conn=persist.connection() 實際編程中用過的連接直接關閉 conn.close() 即可將連接交還給連接池。

PooledDB使用方法同PersistentDB,只是參數有所不同。

dbapi :數據庫接口
mincached :啓動時開啓的空連接數量
maxcached :連接池最大可用連接數量
maxshared :連接池最大可共享連接數量
maxconnections :最大允許連接數量
blocking :達到最大數量時是否阻塞
maxusage :單個連接最大複用次數
setsession :用於傳遞到數據庫的準備會話,如 [”set name UTF-8″] 。
一個使用過程:

db=pooled.connection()
cur=db.cursor()
cur.execute(sql)
res=cur.fetchone()
cur.close() # or del cur
db.close() # or del db



python不用連接池的MySQL連接方法

import MySQLdb
conn= MySQLdb.connect(host='localhost',user='root',passwd='pwd',db='myDB',port=3306)  
#import pymysql
#conn = pymysql.connect(host='localhost', port='3306', db='game', user='root', password='123456', charset='utf8')
cur=conn.cursor()
SQL="select * from table1"
r=cur.execute(SQL)
r=cur.fetchall()
cur.close()
conn.close()


用連接池後的連接方法

import MySQLdb
from DBUtils.PooledDB import PooledDB
pool = PooledDB(MySQLdb,5,host='localhost',user='root',passwd='pwd',db='myDB',port=3306) #5爲連接池裏的最少連接數

conn = pool.connection()  #以後每次需要數據庫連接就是用connection()函數獲取連接就好了
cur=conn.cursor()
SQL="select * from table1"
r=cur.execute(SQL)
r=cur.fetchall()
cur.close()
conn.close()


DBUtils下載地址:https://pypi.python.org/pypi/DBUtils/

測試代碼:

import sys
import threading
import MySQLdb
import DBUtils.PooledDB

connargs = { "host":"localhost", "user":"user1", "passwd":"123456", "db":"test" }
def test(conn):
    try:
        cursor = conn.cursor()
        count = cursor.execute("select * from users")
        rows = cursor.fetchall()
        for r in rows: pass
    finally:
        conn.close()
        
def testloop():
    print ("testloop")
    for i in range(1000):
        conn = MySQLdb.connect(**connargs)
        test(conn)
        
def testpool():
    print ("testpool")
    pooled = DBUtils.PooledDB.PooledDB(MySQLdb, **connargs)
    for i in range(1000):
        conn = pooled.connection()
        test(conn)
        
def main():
    t = testloop if len(sys.argv) == 1 else testpool
    for i in range(10):
        threading.Thread(target = t).start()
        
if __name__ == "__main__":
    main()


看看 10 線程的測試結果。

$ time ./main.py  
testloop  
testloop  
testloop  
testloop  
testloop  
testloop  
testloop  
testloop  
testloop  
testloop  
real    0m4.471s  
user    0m0.570s  
sys     0m4.670s  
$ time ./main.py -l  
testpool  
testpool  
testpool  
testpool  
testpool  
testpool  
testpool  
testpool  
testpool  
testpool  
real    0m2.637s  
user    0m0.320s  
sys     0m2.750s  


雖然測試方式不是很嚴謹,但從測試結果還是能感受到 DBUtils 帶來的性能提升。當然,我們我們也可以在 testloop() 中一直重複使用一個不關閉的 Connection,但這卻不適合實際開發時的情形。

DBUtils 提供了幾個參數,便於我們更好地調整資源利用。

DBUtils.PooledDB.PooledDB(self, creator,   
    mincached=0, maxcached=0, maxshared=0, maxconnections=0, blocking=False, maxusage=None,   
    setsession=None, failures=None, *args, **kwargs)  
Docstring:  
    Set up the DB-API 2 connection pool.  
    creator: either an arbitrary function returning new DB-API 2  
        connection objects or a DB-API 2 compliant database module  
    mincached: initial number of idle connections in the pool  
        (0 means no connections are made at startup)  
    maxcached: maximum number of idle connections in the pool  
        (0 or None means unlimited pool size)  
    maxshared: maximum number of shared connections  
        (0 or None means all connections are dedicated)  
        When this maximum number is reached, connections are  
        shared if they have been requested as shareable.  
    maxconnections: maximum number of connections generally allowed  
        (0 or None means an arbitrary number of connections)  
    blocking: determines behavior when exceeding the maximum  
        (if this is set to true, block and wait until the number of  
        connections decreases, otherwise an error will be reported)  
    maxusage: maximum number of reuses of a single connection  
        (0 or None means unlimited reuse)  
        When this maximum usage number of the connection is reached,  
        the connection is automatically reset (closed and reopened).  
    setsession: optional list of SQL commands that may serve to prepare  
        the session, e.g. ["set datestyle to ...", "set time zone ..."]  
    failures: an optional exception class or a tuple of exception classes  
        for which the connection failover mechanism shall be applied,  
        if the default (OperationalError, InternalError) is not adequate  
    args, kwargs: the parameters that shall be passed to the creator  
        function or the connection constructor of the DB-API 2 module  

DBUtils 僅提供給了連接池管理,實際的數據庫操作依然是由符合 DB-API 2 標準的目標數據庫模塊完成的。
 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章