#Coding:utf-8ImportThreadingImportPymysql fromDbutils.pooleddbImportPOOLEDDB fromApp.common.file_configImportGet_configclassDbpool (object): _instance_lock=Threading. Lock ()def __init__(self):if notHasattr (Dbpool,"Pool"): Dbpool.mysql_pool ()Else: Pass def __new__(CLS, *args, * *Kwargs):if notHasattr (Dbpool,"_instance"): With Dbpool._instance_lock:if notHasattr (Dbpool,"_instance"): Dbpool._instance= object.__new__(CLS, *args, * *Kwargs)returndbpool._instance @staticmethoddefMysql_pool (): Host= Get_config ('Database','Mysql_host') Port= Int (Get_config ('Database','Mysql_port')) User= Get_config ('Database','Mysql_username') passwd= Get_config ('Database','Mysql_password') DB= Get_config ('Database','mysql_db') Dbpool.pool=Pooleddb (creator=pymysql,#modules that use linked databasesmincached=2,#at least 0 of the free links created in the link pool are not created when initializingMaxcached=6,#most idle links in the link pool, 0 and none are not limitedMaxshared=3, #The maximum number of links shared in a linked pool, 0 and none means sharing all. PS: Useless, because Pymysql and mysqldb modules such as threadsafety are 1, all values regardless of set to how much, _maxcached forever is 0, so forever is all links are shared. Maxconnections=8,#the maximum number of connections allowed for a connection pool, 0 and none means no limit on the number of connectionsBlocking=true,#If there are no available connections in the connection pool, wait is blocked. True, wait, False, not wait and then errorMaxusage=none,#the maximum number of times a link is reused, none means unlimitedSetsession=none,#a list of commands to execute before starting the session. such as: ["Set Datestyle to ...", "Set time zone ..."] #Ping the MySQL server to check if the service is available. # for example: 0 = None = never, 1 = default = Whenever it is requested, 2 = When a cursor is created, 4 = When a query is executed, 7 = Alwayshost=Host, Port=Port, User=User, passwd=passwd, DB=db, Use_unicode=False, CharSet='UTF8' )
MySQL data connection pool under Python--Singleton