1. Installing Pymysql
PIP3 Install Pymysql
2. Install the Dbutils Open Source Tool Library
PIP3 Install Dbutils
3. Mode one:
fromDbutils.persistentdbImportPersistentdbImportPymysqlpool=Persistentdb (creator=pymysql,#modules that use linked databasesMaxusage=none,#the maximum number of times a link is reused, none means unlimitedSetsession=[],#a list of commands to execute before starting the session. such as: ["Set Datestyle to ...", "Set time zone ..."]ping=0,#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 = Alwayscloseable=False,#If False, Conn.close () is actually ignored for the next use, and the link is automatically closed when the thread is closed. If True, Conn.close () closes the link, and then calls Pool.connection again with an error, because the connection is actually closed (pool.steady_connection () can get a new link)Threadlocal=none,#This thread is exclusive to the object that holds the linked object, if the linked object is resethost='127.0.0.1', Port=3306, the user='Root', Password='123', Database='Pooldb', CharSet='UTF8')deffunc ():#conn = steadydbconnection ()conn =pool.connection () cursor=conn.cursor () cursor.execute ('SELECT * from TB1') Result=Cursor.fetchall () cursor.close () Conn.close ( )#not really closed, but fake off. conn = Pymysql.connect () conn.close ()Conn=pool.connection () cursor=conn.cursor () cursor.execute ('SELECT * from TB1') Result=Cursor.fetchall () cursor.close () Conn.close ( )ImportThreading forIinchRange (10): T= Threading. Thread (target=func) T.start ()
4. Mode two:
Import TimeImportPymysqlImportThreading fromDbutils.pooleddbImportPooleddb, Shareddbconnectionpool=Pooleddb (creator=pymysql,#modules that use linked databasesMaxconnections=6,#the maximum number of connections allowed for a connection pool, 0 and none means no limit on the number of connectionsmincached=2,#at least 0 of the free links created in the link pool are not created when initializingmaxcached=5,#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. Blocking=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=[],#a list of commands to execute before starting the session. such as: ["Set Datestyle to ...", "Set time zone ..."]ping=0,#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='127.0.0.1', Port=3306, the user='Root', Password='123', Database='Pooldb', CharSet='UTF8')deffunc ():#detects if the number of currently running connections is less than the maximum number of links, if not less than: Waits or reports raise Toomanyconnections exception #otherwise #gets the link steadydbconnection in the link created when the initialization is prioritized. #The Steadydbconnection object is then encapsulated into the pooleddedicateddbconnection and returned. #if the link you initially created is not linked, create a Steadydbconnection object and encapsulate it in pooleddedicateddbconnection and return. #once the link is closed, the connection is returned to the connection pool for subsequent threads to continue to use. #pooleddedicateddbconnectionconn =pool.connection ()#print (th, ' link was taken away ', Conn1._con) #print (Th, ' currently in the pool ', Pool._idle_cache, ' \ r \ n ')cursor=conn.cursor () cursor.execute ('SELECT * from TB1') Result=Cursor.fetchall () conn.close () Conn=pool.connection ()#print (th, ' link was taken away ', Conn1._con) #print (Th, ' currently in the pool ', Pool._idle_cache, ' \ r \ n ')cursor=conn.cursor () cursor.execute ('SELECT * from TB1') Result=Cursor.fetchall () conn.close () func ()
Python Flask Database Connection Pool