カスタムMysql接続プールの開発
サードパーティ製パッケージの使用
https://pypi.python.org/pypi/DBUtils
tar -zxvf *.tar.gz * python3 setup.py build && python3 setup.py install
https://pypi.python.org/pypi/DBUtils
tar -zxvf *.tar.gz * python3 setup.py build && python3 setup.py install
import time
import pymysql
import threading
from DBUtils.PooledDB import PooledDB, SharedDBConnection
POOL = PooledDB(
creator=pymysql, #
maxconnections=6, # ,0 None
mincached=2, # , ,0
maxcached=5, # ,0 None
maxshared=3, # ,0 None 。PS: , pymysql MySQLdb threadsafety 1, ,_maxcached 0, 。
blocking=True, # , 。True, ;False,
maxusage=None, # ,None
setsession=[], # 。 :["set datestyle to ...", "set time zone ..."]
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='123',
database='pooldb',
charset='utf8'
)
def func():
# , : raise TooManyConnections
#
# SteadyDBConnection。
# SteadyDBConnection PooledDedicatedDBConnection 。
# , SteadyDBConnection , PooledDedicatedDBConnection 。
# , 。
conn = POOL.connection()
# print(th, ' ', conn1._con)
# print(th, ' ', pool._idle_cache, '\r
')
cursor = conn.cursor()
cursor.execute('select * from tb1')
result = cursor.fetchall()
# ,
conn.close()
func()