12,DBUtils - Python数据库连接池

创建数据库连接池:

 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,
15     # 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
16     host=‘127.0.0.1‘,
17     port=3306,
18     user=‘root‘,
19     password=‘123‘,
20     database=‘pooldb‘,
21     charset=‘utf8‘
22 )

创建数据库连接池

使用数据库连接池:

 1 def func():
 2     # 检测当前正在运行连接数的是否小于最大链接数,如果不小于则:等待或报raise TooManyConnections异常
 3     # 否则
 4     # 则优先去初始化时创建的链接中获取链接 SteadyDBConnection。
 5     # 然后将SteadyDBConnection对象封装到PooledDedicatedDBConnection中并返回。
 6     # 如果最开始创建的链接没有链接,则去创建一个SteadyDBConnection对象,再封装到PooledDedicatedDBConnection中并返回。
 7     # 一旦关闭链接后,连接就返回到连接池让后续线程继续使用。
 8     conn = POOL.connection()
 9
10     cursor = conn.cursor()
11     cursor.execute(‘select * from tb1‘)
12     result = cursor.fetchall()
13     conn.close()

使用数据库连接池中的链接

自制sqlhelper

 1 class MySQLhelper(object):
 2     def __init__(self, host, port, dbuser, password, database):
 3         self.pool = PooledDB(
 4             creator=pymysql,  # 使用链接数据库的模块
 5             maxconnections=6,  # 连接池允许的最大连接数,0和None表示不限制连接数
 6             mincached=2,  # 初始化时,链接池中至少创建的空闲的链接,0表示不创建
 7             maxcached=5,  # 链接池中最多闲置的链接,0和None不限制
 8             maxshared=3,
 9             # 链接池中最多共享的链接数量,0和None表示全部共享。PS: 无用,因为pymysql和MySQLdb等模块的 threadsafety都为1,所有值无论设置为多少,_maxcached永远为0,所以永远是所有链接都共享。
10             blocking=True,  # 连接池中如果没有可用连接后,是否阻塞等待。True,等待;False,不等待然后报错
11             maxusage=None,  # 一个链接最多被重复使用的次数,None表示无限制
12             setsession=[],  # 开始会话前执行的命令列表。如:["set datestyle to ...", "set time zone ..."]
13             ping=0,
14             # 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=host,
16             port=int(port),
17             user=dbuser,
18             password=password,
19             database=database,
20             charset=‘utf8‘
21         )
22
23     def create_conn_cursor(self):
24         conn = self.pool.connection()
25         cursor = conn.cursor(pymysql.cursors.DictCursor)
26         return conn,cursor
27
28     def fetch_all(self, sql, args):
29         conn,cursor = self.create_conn_cursor()
30         cursor.execute(sql,args)
31         result = cursor.fetchall()
32         cursor.close()
33         conn.close()
34         return result
35
36
37     def insert_one(self,sql,args):
38         conn,cursor = self.create_conn_cursor()
39         res = cursor.execute(sql,args)
40         conn.commit()
41         print(res)
42         conn.close()
43         return res
44
45     def update(self,sql,args):
46         conn,cursor = self.create_conn_cursor()
47         res = cursor.execute(sql,args)
48         conn.commit()
49         print(res)
50         conn.close()
51         return res
52
53
54 sqlhelper = MySQLhelper("127.0.0.1", 3306, "root", "1233121234567", "dragon")
55
56 # sqlhelper.fetch_all("select * from user where id=%s",(1))
57
58 # sqlhelper.insert_one("insert into user VALUES (%s,%s)",("jinwangba",4))
59
60 # sqlhelper.update("update user SET name=%s WHERE  id=%s",("yinwangba",1))

好使不好使,试试就知道了

原文地址:https://www.cnblogs.com/feifeifeisir/p/10548025.html

时间: 2024-08-28 11:18:30

12,DBUtils - Python数据库连接池的相关文章

Python数据库连接池模块-----DBUtils使用

python的数据库连接池实现----DBUtils DBUtils 属于WebWare项目的数据库连接池实现模块,用于对数据库连接线程化,使可以安全和有效的访问数据库的模块 DBUtils实际上是一个包含两个子模块的Python包,一个用于连接DB-API 2模块,另一个用于连接典型的PyGreSQL模块. 全局的DB-API 2变量 SteadyDB.py 用于稳定数据库连接 PooledDB.py 连接池 PersistentDB.py 维持持续的数据库连接 SimplePooledDB.

【Python数据库连接池基本用法】 -- 2019-08-11 19:27:14

目录 基本用法 自制sqlhelper 原文: http://106.13.73.98/__/121/ @(Python数据库连接池) 确保已安装:pip install DBUtils *** 基本用法 先准备些数据 # 建了个表 create table userinfo( id int, name varchar(32), age int(3) ); # 插入记录 insert into userinfo values (1, 'user01', 21), (2, 'user02', 22

【Python数据库连接池基本用法】 𣹿

目录 基本用法 自制sqlhelper 原文: http://blog.gqylpy.com/gqy/346 @(Python数据库连接池) 确保已安装:pip install DBUtils *** 基本用法 先准备些数据 # 建了个表 create table userinfo( id int, name varchar(32), age int(3) ); # 插入记录 insert into userinfo values (1, 'user01', 21), (2, 'user02',

Python数据库连接池DButils

DButils是python的一个实现数据库连接池的模块 两种模式: 1.为每一个线程创建一个链接,即使线程即使调用了close()方法,也不会关闭,只是把线程放到连接池,供自己再次使用,当连接关闭时,线程连接自动关闭. 1 from DBUtils.PersistentDB import PersistentDB 2 import pymysql 3 PooL = PersistentDB( 4 creator = pymysql, #使用链接数据库的模块 5 maxusage = None,

Python 数据库连接池DButils

常规的数据库链接存在的问题: 场景一: 缺点:每次请求反复创建数据库连接,连接数太多 import pymysql def index(): conn = pymysql.connect() cursor = conn.cursor() cursor.execute('select * from tb where id > %s',[5,]) result = cursor.fetchall() cursor.close() conn.close() print(result) def upda

Python数据库连接池实例——PooledDB

不用连接池的MySQL连接方法 import MySQLdbconn= MySQLdb.connect(host='localhost',user='root',passwd='pwd',db='myDB',port=3306) cur=conn.cursor() SQL="select * from table1" r=cur.execute(SQL) r=cur.fetchall() cur.close() conn.close() 用连接池后的连接方法 import MySQLd

Python数据库连接池实例——PooledDB

不用连接池的MySQL连接方法 import MySQLdb conn= MySQLdb.connect(host='localhost',user='root',passwd='pwd',db='myDB',port=3306) cur=conn.cursor() SQL="select * from table1" r=cur.execute(SQL) r=cur.fetchall() cur.close() conn.close() 用连接池后的连接方法 import MySQL

Python实现Mysql数据库连接池

python连接Mysql数据库: Python编程中可以使用MySQLdb进行数据库的连接及诸如查询/插入/更新等操作,但是每次连接MySQL数据库请求时,都是独立的去请求访问,相当浪费资源,而且访问数量达到一定数量时,对mysql的性能会产生较大的影响.因此,实际使用中,通常会使用数据库的连接池技术,来访问数据库达到资源复用的目的. python的数据库连接池包 DBUtils: DBUtils是一套Python数据库连接池包,并允许对非线程安全的数据库接口进行线程安全包装.DBUtils来

Python 使用 PyMysql、DBUtils 创建连接池提升性能

Python 使用 PyMysql.DBUtils 创建连接池提升性能 Python 编程中可以使用 PyMysql 进行数据库的连接及诸如查询/插入/更新等操作,但是每次连接 MySQL 数据库请求时,都是独立的去请求访问,相当浪费资源,而且访问数量达到一定数量时,对 mysql 的性能会产生较大的影响.因此,实际使用中,通常会使用数据库的连接池技术,来访问数据库达到资源复用的目的. 解决方案:DBUtils DBUtils 是一套 Python 数据库连接池包,并允许对非线程安全的数据库接口