內容:html
1.DBUtils介紹mysql
2.DBUtils兩種鏈接模式sql
3.DBUtils實際使用數據庫
參考:http://www.cnblogs.com/wupeiqi/articles/8184686.htmlflask
1.DBUtils介紹session
DBUtils是Python的一個用於實現數據庫鏈接池的模塊,咱們可使用DBUtils分別的建立數據庫鏈接池多線程
安裝:pip install DBUtilsapp
爲何要使用數據庫鏈接池:若是沒有鏈接池,使用pymysql來鏈接數據庫時,單線程應用徹底沒有問題,但若是涉及到多線程應用那麼就須要加鎖,一旦加鎖那麼鏈接勢必就會排隊等待,當請求比較多時,性能就會下降了性能
2.DBUtils兩種鏈接模式fetch
兩種鏈接模式:
模式一實例:
1 POOL = PersistentDB( 2 creator=pymysql, # 使用連接數據庫的模塊 3 maxusage=None, # 一個連接最多被重複使用的次數,None表示無限制 4 setsession=[], # 開始會話前執行的命令列表。如:["set datestyle to ...", "set time zone ..."] 5 ping=0, # ping MySQL服務端,檢查是否服務可用。# 如:0 = None = never, 1 = default = whenever it is requested, 2 = when a cursor is created, 4 = when a query is executed, 7 = always 6 closeable=False, # 若是爲False時, conn.close() 實際上被忽略,conn繼續供下次使用,只有在線程關閉時纔會自動關閉鏈接。若是爲True時, conn.close()則關閉連接,那麼再次調用pool.connection時就會報錯,由於已經真的關閉了鏈接(pool.steady_connection()能夠獲取一個新的連接) 7 threadlocal=None, # 本線程獨享值得對象,用於保存連接對象,若是連接對象被重置 8 host='127.0.0.1', 9 port=3306, 10 user='root', 11 password='root', 12 database='test', 13 charset='utf8' 14 ) 15 16 def func(): 17 conn = POOL.connection(shareable=False) 18 cursor = conn.cursor() 19 cursor.execute('select * from users') 20 result = cursor.fetchall() 21 cursor.close() 22 conn.close() 23 24 func()
模式二實例:
1 import time 2 import pymysql 3 import threading 4 from DBUtils.PooledDB import PooledDB, SharedDBConnection 5 POOL = PooledDB( 6 creator=pymysql, # 使用連接數據庫的模塊 7 maxconnections=6, # 鏈接池容許的最大鏈接數,0和None表示不限制鏈接數 8 mincached=2, # 初始化時,連接池中至少建立的空閒的連接,0表示不建立 9 maxcached=5, # 連接池中最多閒置的連接,0和None不限制 10 maxshared=3, # 連接池中最多共享的連接數量,0和None表示所有共享。PS: 無用,由於pymysql和MySQLdb等模塊的 threadsafety都爲1,全部值不管設置爲多少,_maxcached永遠爲0,因此永遠是全部連接都共享。 11 blocking=True, # 鏈接池中若是沒有可用鏈接後,是否阻塞等待。True,等待;False,不等待而後報錯 12 maxusage=None, # 一個連接最多被重複使用的次數,None表示無限制 13 setsession=[], # 開始會話前執行的命令列表。如:["set datestyle to ...", "set time zone ..."] 14 ping=0, # ping MySQL服務端,檢查是否服務可用。# 如:0 = None = never, 1 = default = whenever it is requested, 2 = when a cursor is created, 4 = when a query is executed, 7 = always 15 host='127.0.0.1', 16 port=3306, 17 user='root', 18 password='root', 19 database='test', 20 charset='utf8' 21 ) 22 23 24 def func(): 25 # 檢測當前正在運行鏈接數的是否小於最大連接數,若是不小於則等待或報raise TooManyConnections異常 26 # 不然 27 # 則優先去初始化時建立的連接中獲取連接 SteadyDBConnection。 28 # 而後將SteadyDBConnection對象封裝到PooledDedicatedDBConnection中並返回。 29 # 若是最開始建立的連接沒有連接,則去建立一個SteadyDBConnection對象,再封裝到PooledDedicatedDBConnection中並返回。 30 # 一旦關閉連接後,鏈接就返回到鏈接池讓後續線程繼續使用。 31 conn = POOL.connection() 32 33 # print(th, '連接被拿走了', conn1._con) 34 # print(th, '池子裏目前有', pool._idle_cache, '\r\n') 35 36 cursor = conn.cursor() 37 cursor.execute('select * from tb1') 38 result = cursor.fetchall() 39 conn.close() 40 41 42 func()
3.DBUtils實際使用
下面是DBUtils在flask中的實際使用:
1 # encoding: utf-8 2 # __author__ = "wyb" 3 # date: 2018/11/7 4 import pymysql 5 from DBUtils.PooledDB import PooledDB 6 from flask import Flask 7 8 POOL = PooledDB( 9 creator=pymysql, # 使用連接數據庫的模塊 10 maxconnections=6, # 鏈接池容許的最大鏈接數,0和None表示不限制鏈接數 11 mincached=2, # 初始化時,連接池中至少建立的空閒的連接,0表示不建立 12 maxcached=5, # 連接池中最多閒置的連接,0和None不限制 13 maxshared=3, # 連接池中最多共享的連接數量,0和None表示所有共享。PS: 無用,由於pymysql和MySQLdb等模塊的 threadsafety都爲1,全部值不管設置爲多少,_maxcached永遠爲0,因此永遠是全部連接都共享。 14 blocking=True, # 鏈接池中若是沒有可用鏈接後,是否阻塞等待。True,等待;False,不等待而後報錯 15 maxusage=None, # 一個連接最多被重複使用的次數,None表示無限制 16 setsession=[], # 開始會話前執行的命令列表。如:["set datestyle to ...", "set time zone ..."] 17 ping=0, # ping MySQL服務端,檢查是否服務可用。# 如:0 = None = never, 1 = default = whenever it is requested, 2 = when a cursor is created, 4 = when a query is executed, 7 = always 18 host='127.0.0.1', 19 port=3306, 20 user='root', 21 password='root', 22 database='test', 23 charset='utf8' 24 ) 25 26 27 # SQL相關操做寫這裏面 -> 增刪改查 28 class SQLHelper(object): 29 def __init__(self): 30 self.conn = POOL.connection() 31 self.cursor = self.conn.cursor() 32 33 def close(self): 34 self.cursor.close() 35 self.conn.close() 36 37 def fetch_one(self, sql, args): 38 self.cursor.execute(sql, args) 39 result = self.cursor.fetchone() 40 self.close() 41 42 return result 43 44 def fetch_all(self, sql, args): 45 self.cursor.execute(sql, args) 46 result = self.cursor.fetchall() 47 self.close() 48 49 return result 50 51 52 app = Flask(__name__) 53 54 55 @app.route("/") 56 def hello(): 57 obj = SQLHelper() 58 result = obj.fetch_all('select * from users', []) 59 print(result) 60 return 'hello world' 61 62 63 if __name__ == '__main__': 64 app.run(debug=True, host='0.0.0.0', port=8888)