Python數據庫鏈接池DButils

  DButils是python的一個實現數據庫鏈接池的模塊python

  兩種模式:mysql

  1.爲每個線程建立一個連接,即便線程即便調用了close()方法,也不會關閉,只是把線程放到鏈接池,供本身再次使用,當鏈接關閉時,線程鏈接自動關閉。sql

 1 from DBUtils.PersistentDB import PersistentDB  2 import pymysql  3 PooL = PersistentDB(  4     creator = pymysql,  #使用連接數據庫的模塊
 5     maxusage = None, #一個連接最多被使用的次數,None表示無限制
 6     setsession = [], #開始會話前執行的命令
 7     ping = 0, #ping MySQL服務端,檢查服務是否可用
 8     closeable = False, #conn.close()實際上被忽略,供下次使用,直到線程關閉,自動關閉連接,而等於True時,conn.close()真的被關閉
 9     threadlocal = None, # 本線程獨享值的對象,用於保存連接對象
10     host = '127.0.0.1', 11     port = 3306, 12     user = 'root', 13     password = '123', 14     database = 'ok1', 15     charset = 'utf8'
16 ) 17 def func(): 18     conn = PooL.connection() 19     cursor = conn.cursor() 20     cursor.execute('select * from book') 21     result = cursor.fetchall() 22     print(result) 23  cursor.close() 24  conn.close() 25 import threading 26 for i in range(5): 27     t = threading.Thread(target=func) 28     t.start()
模式一

  2.建立一批鏈接到鏈接池,供全部線程共享使用數據庫

  注意:因爲pymysql,mysqlDB中的threadsafety值爲1,全部線程共享鏈接session

 1 import time  2 import pymysql  3 import threading  4 from DBUtils.PooledDB import PooledDB,SharedDBConnection  5 
 6 POOL = PooledDB(  7     creator = pymysql, #使用連接數據庫的模塊
 8     maxconnections = 6,  #鏈接池容許的最大鏈接數,0和None表示沒有限制
 9     mincached = 2, #初始化時,鏈接池至少建立的空閒的鏈接,0表示不建立
10     maxcached = 5, #鏈接池空閒的最多鏈接數,0和None表示沒有限制
11     maxshared = 3, #鏈接池中最多共享的鏈接數量,0和None表示所有共享,ps:其實並無什麼用,由於pymsql和MySQLDB等模塊中的threadsafety都爲1,全部值不管設置多少,_maxcahed永遠爲0,因此永遠是全部連接共享
12     blocking = True, #連接池中若是沒有可用共享鏈接後,是否阻塞等待,True表示等待,False表示不等待而後報錯
13     setsession = [],#開始會話前執行的命令列表
14     ping = 0,#ping Mysql 服務端,檢查服務是否可用
15     host = '127.0.0.1', 16     port = 3306, 17     user = 'root', 18     password = '123', 19     database = 'ok1', 20     charset = 'utf8'
21 ) 22 def func(): 23     #檢測當前正在運行的鏈接數是否小於最大的鏈接數,若是不小於則等待鏈接或者拋出raise TooManyConnections異常
24     #不然優先去初始化時建立的鏈接中獲取鏈接SteadyDBConnection
25     #而後將SteadyDBConnection對象封裝到PooledDedicatedDBConnection中並返回
26     #若是最開始建立的鏈接沒有連接,則去建立SteadyDBConnection對象,再封裝到PooledDedicatedDBConnection中並返回
27     #一旦關閉連接後,鏈接就返回到鏈接池讓後續線程繼續使用
28     conn = POOL.connection() 29     cursor = conn.cursor() 30     cursor.execute('select * from book') 31     result = cursor.fetchall() 32     print(result) 33  conn.close() 34 func()
模式二

  PS:關於pymysql模塊,若是沒有單線程的狀況,鏈接MySQL數據庫沒有問題,但若是要是多線程,就須要加鎖,一旦加鎖,後面的線程就得等待,勢必會下降使用效率。多線程

 1 import pymysql  2 import threading  3 from threading import RLock  4 LOCK = RLock()  5 CONN = pymysql.connect(host='127.0.0.1',  6                        port = 3306,  7                        user = 'root',  8                        password = '123',  9                        database = 'ok1', 10                        charset = 'utf8'
11  ) 12 def task(arg): 13  with LOCK: 14         cursor = CONN.cursor() 15         cursor.execute('select * from book') 16         result = cursor.fetchall() 17  cursor.close() 18         print(result) 19 for i in range(10): 20     t = threading.Thread(target=task,args=(i,)) 21     t.start()
加鎖
 1 import pymysql  2 import threading  3 CONN = pymysql.connect(host='127.0.0.1',  4                        port=3306,  5                        user='root',  6                        password='123',  7                        database='ok1',  8                        charset='utf8')  9 
10 def task(arg): 11     cursor = CONN.cursor() 12     cursor.execute('select * from book') 13     result = cursor.fetchall() 14  cursor.close() 15     print(result) 16 for i in range(10):  #1時不會報錯
17     t = threading.Thread(target=task, args=(i,)) 18     t.start()
不加鎖
相關文章
相關標籤/搜索