flask 數據庫連接池
阿新 • • 發佈:2019-01-15
小白 永遠 [] xcache 關閉 數據庫連接池 hide always 數據庫連接
- python數據庫連接池
- 如果每一個請求過來就創建一個鏈接這樣效率會很低,所以需要建立連接池
- DBUtils是python的一個用於實現數據庫連接池的模塊
# pip3 安裝 pip3 install DBUtils
- 創建數據庫連接池
- 為每個線程創建一個連接,線程即使調用了close方法,也不會關閉,只是把連接重新放到連接池,供自己線程再次使用。當線程終止時,連接自動關閉
import pymysql from DBUtils.PooledDB import PooledDB POOL = PooledDB( creator=pymysql, # 使用鏈接數據庫的模塊 maxconnections=6, # 連接池允許的最大連接數,0和None表示不限制連接數 mincached=2, # 初始化時,鏈接池中至少創建的空閑的鏈接,0表示不創建 maxcached=5, # 鏈接池中最多閑置的鏈接,0和None不限制 maxshared=3, # 鏈接池中最多共享的鏈接數量,0和None表示全部共享。PS: 無用,因為pymysql和MySQLdb等模塊的 threadsafety都為1,所有值無論設置為多少,_maxcached永遠為0,所以永遠是所有鏈接都共享。 blocking=True, #連接池中如果沒有可用連接後,是否阻塞等待。True,等待;False,不等待然後報錯 maxusage=None, # 一個鏈接最多被重復使用的次數,None表示無限制 setsession=[], # 開始會話前執行的命令列表。如:["set datestyle to ...", "set time zone ..."] 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
# 下面為mysql連接配置 host="127.0.0.1", port=3306, user="root", password="", charset="utf8", db="userinfo" )
pymysql基礎操作點我:https://www.cnblogs.com/lzmdbk/p/10269042.html
- 簡單查詢
- 註意檢測當前正在運行連接數的是否小於最大鏈接數,如果不小於則:等待或報raise TooManyConnections異常
conn = POOL.connection() cur = conn.cursor(cursor=pymysql.cursors.DictCursor) sql = "select * from users WHERE name=‘小白‘ and age=16 " res = cur.execute(sql) print(cur.fetchall()) conn.close()
結果:
[{‘id‘: 3, ‘name‘: ‘小白‘, ‘age‘: 16}]
- 自主增刪改查工具
dbpool.py
import pymysql from DBUtils.PooledDB import PooledDB POOL = PooledDB( creator=pymysql, maxconnections=6, mincached=2, maxcached=5, maxshared=3, blocking=True, maxusage=None, setsession=[], ping=0, host="127.0.0.1", port=3306, user="root", password="", charset="utf8", db="userinfo" )dbpool.py
sqlhelper.py
from dbpool import POOL import pymysql def create_conn(): # 創建連接 conn = POOL.connection() cursor = conn.cursor(cursor=pymysql.cursors.DictCursor) return conn,cursor def close_conn(conn,cursor): # 關閉連接 cursor.close() conn.close() def fetch_all(sql,args=None): # 查詢全部 conn,cursor = create_conn() cursor.execute(sql,args) res = cursor.fetchall() close_conn(conn,cursor) return res # sql = "select * from users" # # print(fetch_all(sql)) # [{‘id‘: 1, ‘name‘: ‘小明‘, ‘age‘: 18}, {‘id‘: 2, ‘name‘: ‘小黑‘, ‘age‘: 17}, {‘id‘: 3, ‘name‘: ‘小白‘, ‘age‘: 16}] def fetch_one(sql,args): # 單個查詢 conn,cursor = create_conn() cursor.execute(sql,args) res = cursor.fetchone() close_conn(conn,cursor) return res # sql = "select * from users where name=%s and age=%s" # # print(fetch_one(sql,("小明",18))) # {‘id‘: 1, ‘name‘: ‘小明‘, ‘age‘: 18} def insert(sql,args): conn,cursor = create_conn() res = cursor.execute(sql,args) conn.commit() close_conn(conn, cursor) return res # sql = "insert into users(id,name,age) value (%s,%s,%s)" # # print(insert(sql,(4,"小王",20))) def delete(sql,args): conn, cursor = create_conn() res = cursor.execute(sql, args) conn.commit() close_conn(conn, cursor) return res sql = "delete from users where id = %s" print(delete(sql,4))sqlhelper.py
flask 數據庫連接池