データベース接続プーリング

データベース接続プーリングとは何ですか?そして、役割?

'''
数据库链接池的基本原理:为数据库建立一个缓冲池,预先在池中放入一定数量的数据库链接管道,需要时,从链接池中取出管道进行使用,操作完毕后,再将链接放回到池子中,从而避
免了频繁的链接数据库,资源的申请和释放的性能损耗            

由于数据库链接得到重用,避免了频翻创建,释放链接引起的大量性能开销,在减少系统消耗的基础上,增进了系统环境的平稳性,更快的系统响应速度
'''

データベース接続プーリング機能マルチスレッド版

from DBUtils.PooledDB import PooledDB
from concurrent.futures import ThreadPoolExecutor


POOL = PooledDB(
            creator=pymysql,    # 使用链接数据库的模块
            maxconnections=6,   # 连接池允许的最大连接数,0和None表示不限制连接数
            mincached=2,        # 初始化时,链接池中至少创建的链接,0表示不创建
            
            blocking=True,      #连接池中如果没有可用连接后,
                                #是否阻塞等待。True,等待;False,不等待然后报错

            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
            host='127.0.0.1',
            port=3306,
            user='root',
            password='123456',
            database='flask',
            charset='utf8'
        )
        
def task(num):
    # 去连接池中获取一个连接
    conn = POOL.connection()
    # 获取游标
    cursor = conn.cursor()
    # cursor.execute('select * from book')
    cursor.execute('select sleep(3)')
    result = cursor.fetchall()
    cursor.close()
    # 关闭链接后将连接放会到连接池
    conn.close()
    print(num,'------------>',result)


from threading import Thread
for i in range(57):
    t = Thread(target=task,args=(i,))
    t.start() 
    
    
# 另一种线程池
with ThreadPoolExecutor(max_workers=10) as t:
    task = t.submit(task,1)    

クラスビュー、データベース接続プーリングのバージョン

import pymysql
from DBUtils.PooledDB import PooledDB


class SqlHelper(object):
    def __init__(self):
        self.pool = PooledDB(
            creator=pymysql,    # 使用链接数据库的模块
            
            maxconnections=6,   #连接池允许的最大连接数,0和None表示不限制连接数           
            mincached=2,        #初始化时,链接池中至少创建的链接,0表示不创建
            
            blocking=True,      # 连接池中如果没有可用连接后,是否阻塞等待。
                                #True,等待;False,不等待然后报错
                                
                                
            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
            host='127.0.0.1',
            port=3306,
            user='root',
            password='123456',
            database='flask',
            charset='utf8'
        )

    def open(self):
        conn = self.pool.connection()
        cursor = conn.cursor(pymysql.cursors.DictCursor)
        return conn, cursor

    def close(self, conn, cursor):
        cursor.close()
        conn.close()

    def fetchall(self, sql, *args):
        conn, cursor = self.open()
        cursor.execute(sql, args)
        conn.commit()
        result = cursor.fetchall()
        self.close(conn, cursor)
        return result

    def fetchone(self, sql, *args):
        conn, cursor = self.open()
        cursor.execute(sql, args)
        conn.commit()
        result = cursor.fetchone()
        self.close(conn, cursor)
        return result

    def insert(self, sql, *args):
        conn, cursor = self.open()
        cursor.execute(sql, *args)
        conn.commit()
        result = cursor.fetchall()
        self.close(conn, cursor)
        return result


db = SqlHelper()

おすすめ

転載: www.cnblogs.com/daviddd/p/11913973.html