This repository has been archived on 2024-09-30. You can view files and clone it, but cannot push or open issues/pull-requests.
SmartRollCall/db/connection.py

23 lines
897 B
Python
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

import pymysql
from dbutils.pooled_db import PooledDB
from config import DB_CONFIG
class MySQLPool:
def __init__(self):
# 初始化时建立数据库连接池
self.pool = PooledDB(
creator=pymysql, # 使用链接数据库的模块
maxconnections=6, # 连接池允许的最大连接数
mincached=2, # 初始化时,连接池中至少创建的空闲的连接
maxcached=5, # 连接池中最多闲置的连接
blocking=True, # 连接池中如果没有可用连接后是否阻塞等待
maxusage=None, # 一个连接最多被重复使用的次数None表示无限制
setsession=[], # 开始会话前执行的命令列表
ping=0,
**DB_CONFIG
)
def get_connection(self):
# 从连接池中获取一个连接
return self.pool.connection()