python數(shù)據(jù)庫PooledDB連接池初始化使用示例
更新時間:2023年08月07日 10:08:33 作者:擼小魚
這篇文章主要為大家介紹了python數(shù)據(jù)庫PooledDB連接池初始化使用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
版本環(huán)境
- python 3.7
- DBUtils 1.3
- mysqlclient 1.4.6
連接池初始化
pool = PooledDB(creator=MySQLdb, mincached=0, maxcached=0, maxshared=0, maxconnections=0, blocking=False,maxusage=None, setsession=None, reset=True, failures=None, ping=1, *args, **kwargs)
參數(shù)說明
- creator
#creator => 任何符合DB-API 2.0規(guī)范的函數(shù)或者兼容的數(shù)據(jù)庫模塊
- mincached
#mincached => 初始化時,鏈接池中至少創(chuàng)建的空閑的鏈接,0表示不創(chuàng)建
- maxcached
#maxcached => 鏈接池中最大閑置的鏈接數(shù)(0和None不限制)
- maxshared
#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
#maxconnections => 允許的最大鏈接數(shù)(0或None表示不限制)
- blocking
#blocking => 鏈接池沒有可用鏈接后,是否阻塞等待。 True表示阻塞等待,直到獲取到鏈接; False不等待,拋異常退出
- maxusage
#maxusage => 同一個鏈接最多被重復(fù)使用的次數(shù)(0和None表示無限制)
- setsession
#setsession => 可選的會話命令:開始會話前執(zhí)行的命令列表。 例如["set datestyle to…","set time zone…"]
- reset
#reset => 當(dāng)連接放回池中時,重置連接的方式,默認為True。 False或者None表示使用begin()開啟了事務(wù)的鏈接,會執(zhí)行回滾; 安全起見,建議使用True,當(dāng)為True時表示所有鏈接都執(zhí)行回滾操作
- failures
#failures => 當(dāng)默認的(OperationalError,InternalError)異常不能滿足要求時, 可以自定義拋出異常:默認為None; 自定義為傳入的為tuple或者issubclass(failures, Exception)
- ping
#ping => 檢查連接是否仍然處于活動狀態(tài)的方式 0 = None = never, 1 = default = whenever fetched from the pool, 2 = when a cursor is created, 4 = when a query is executed, 7 = always, and all other bit combinations of these values
- args, kwargs
#args, kwargs => 傳遞給creator的參數(shù)
使用示例
# -*- coding: utf-8 -*- # @Time : 2020/1/26 0026 20:28 # @Email : lofish@foxmail.com(擼小魚) import MySQLdb import MySQLdb.cursors from DBUtils.PooledDB import PooledDB import datetime class DbManager(object): def __init__(self, host, port, db_name, user_name, password): cmds = ["set names utf8mb4;"] conn_args = {'host': host, 'port': port, 'db': db_name, 'user': user_name, 'passwd': password, 'charset': 'utf8', 'cursorclass': MySQLdb.cursors.DictCursor } # 初始化時,鏈接池中至少創(chuàng)建的空閑的鏈接,0表示不創(chuàng)建,mincached: 5 # 鏈接池中最大閑置的鏈接數(shù)(0和None不限制): 20 self._pool = PooledDB(MySQLdb, mincached=5, maxcached=20, setsession=cmds, **conn_args) def connection(self): return self._pool.connection() _db_manager = None def create_db_manager(host, port, dbname, username, password): global _db_manager if _db_manager is None: _db_manager = DbManager(host, port, dbname, username, password) return _db_manager
以上就是python數(shù)據(jù)庫PooledDB連接池初始化使用示例的詳細內(nèi)容,更多關(guān)于python數(shù)據(jù)庫PooledDB連接池的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Python+PyQt5實現(xiàn)自動化任務(wù)管理
這篇文章主要為大家詳細介紹了如何通過PyQt5構(gòu)建圖形界面,使用Python實現(xiàn)了一個自動化任務(wù)管理系統(tǒng),感興趣的小伙伴可以參考一下2025-04-04淺談numpy中函數(shù)resize與reshape,ravel與flatten的區(qū)別
這篇文章主要介紹了淺談numpy中函數(shù)resize與reshape,ravel與flatten的區(qū)別介紹,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-06-06基于Python實現(xiàn)下載網(wǎng)易音樂代碼實例
這篇文章主要介紹了基于Python實現(xiàn)下載網(wǎng)易音樂代碼實例,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-08-08一文讓你徹底搞懂Python中__str__和__repr__
這篇文章主要介紹了Python中的__str__和__repr__的異同,__str__和__repr__是基本的內(nèi)置方法,文中有詳細的代碼示例,感興趣的同學(xué)可以參考閱讀下2023-05-05Python使用crontab模塊設(shè)置和清除定時任務(wù)操作詳解
這篇文章主要介紹了Python使用crontab模塊設(shè)置和清除定時任務(wù)操作,結(jié)合實例形式分析了centos7平臺上Python安裝、python-crontab模塊安裝,以及基于python-crontab模塊的定時任務(wù)相關(guān)操作技巧,需要的朋友可以參考下2019-04-04