Python DBUtils 连接池对象 (PooledDB)

数据处理用到 mysql, 需要在多进程中的多线程中使用 mysql 的连接
使用到的模块: DBUtils

实现: 

  1. 使用 DBUtils 中的 PooledDB 类来实现.
  2. 自己写一个类, 继承 PooledDB 类, 这样就能使用到 PooledDB 中的其它常用的方法.
  3. 使用单例模式, 确保整个应用服务中只有一个连接池对象.
  4. 使用:  可以定义全局变量初始化连接池对象, 在别的地方获取mysql连接使用.

连接池 demo:

# coding: utf-8
import pymysql
import functools
from seemmo.common.time_func import now_time, today
from conf.config import DB_HOST, DB_PORT, DB_USER, DB_NAME, DB_CHARSET
from pymysql.cursors import DictCursor
from DBUtils.PooledDB import PooledDB

# 获取加密的mysql密码
from seemmo.common.utils import get_pwd
DB_PASSWD = get_pwd("mysql_pwd")


class MysqlPool(PooledDB):
    __instance = None
    __pool = None

    def __new__(cls, *args, **kwargs):
        if not cls.__instance:
            # cls.__instance = object.__new__(cls)
            cls.__instance = super().__new__(cls)
        return cls.__instance

    def __init__(self):
        """创建链接池"""
        PooledDB.__init__(self, pymysql, maxconnections=50, mincached=0, maxcached=0, maxshared=0, blocking=True,
                          host=DB_HOST, port=DB_PORT, user=DB_USER, password=DB_PASSWD, database=DB_NAME,
                          charset=DB_CHARSET, cursorclass=DictCursor)

    def conn(self):
        _conn = self.connection()
        return _conn

使用 (装饰器) :

# 创建连接池对象
mysql_pool = MysqlPool()


# mysql 闭包环境
def db_wrap(method):
    @functools.wraps(method)
    def _wrapper(*args, **kwargs):
        conn = mysql_pool.conn()
        cursor = conn.cursor()
        try:
            conn.begin()
            retval = method(cursor, *args, **kwargs)
            conn.commit()
        except Exception as e:
            conn.rollback()
            raise e
        finally:
            cursor.close()
            conn.close()
        return retval
    return _wrapper


@db_wrap
def create_table(cursor, sql):
    ret = cursor.execute(sql)
    return ret


@db_wrap
def query_by_index(cursor, table_name, start_index, next_index):
    """
    根据 index 范围查询违法记录
    :param cursor: 数据库操作游标
    :param table_name: 需要操作的表名称
    :param start_index: 开始 index
    :param next_index:  截止 index
    :return: 查询结果
    """
    sql = """select * from {} where id > {} and id <= {}""".format(table_name, start_index, next_index)
    cursor.execute(sql)
    res = cursor.fetchall()
    return res

PooledDB的参数:

  1. mincached,最少的空闲连接数,如果空闲连接数小于这个数,pool会创建一个新的连接
  2. maxcached,最大的空闲连接数,如果空闲连接数大于这个数,pool会关闭空闲连接
  3. maxconnections,最大的连接数,
  4. blocking,当连接数达到最大的连接数时,在请求连接的时候,如果这个值是True,请求连接的程序会一直等待,直到当前连接数小于最大连接数,如果这个值是False,会报错,
  5. maxshared 当连接数达到这个数,新请求的连接会分享已经分配出去的连接

在uwsgi中,每个http请求都会分发给一个进程,连接池中配置的连接数都是一个进程为单位的(即上面的最大连接数,都是在一个进程中的连接数),而如果业务中,一个http请求中需要的sql连接数不是很多的话(其实大多数都只需要创建一个连接),配置的连接数都不需要太大。


连接池对性能的提升表现在:
1.在程序创建连接的时候,可以从一个空闲的连接中获取,不需要重新初始化连接,提升获取连接的速度
2.关闭连接的时候,把连接放回连接池,而不是真正的关闭,所以可以减少频繁地打开和关闭连接

DBUtils下载地址:https://pypi.python.org/pypi/DBUtils/

ending ~  

猜你喜欢

转载自www.cnblogs.com/kaichenkai/p/11851597.html