Python database connection pool - assembly DBUtils

DBUtils Python is a module for implementing a database connection pool

This connection pool has two connection modes:

DBUtils provides two external interfaces:
PersistentDB: provide thread specific database connection, and automatically manage connections.
PooledDB: providing inter-thread can share database connections and automatically manage the connection.

PersistentDB mode
each thread to create a connection, even if the calling thread a close method, it will not shut down, just put a link back on connection pooling for their own threads again, when the thread terminates, the link automatically shut down

Copy the code
from DBUtils.PersistentDB Import PersistentDB
Import pymysql
the POOL = PersistentDB (
Creator = pymysql, link database module #
maxusage = None, # link up a number of times is reused, None indicates unlimited
setsession = [], # starting a session before executing the command list.
the ping = 0,
# the ping MySQL server, check whether the service is available.
Closeable = False,
# if is False, conn.close () are effectively ignored for the next use, then thread closed , the link will automatically shut down. If is True, conn.close () closes the link, you'll get an error when calling pool.connection again, as it has been really closed the connection (pool.steady_connection () can get a new Links)
ThreadLocal = None, # this thread exclusive worthy object, which holds a linked object, the object is reset if the link
Host = '127.0.0.1',
Port = 3306,
the User = 'root',
password = '123456',
= Database 'Test',
charset = 'UTF8'
)

def func():
conn = POOL.connection(shareable=False)
cursor = conn.cursor()
cursor.execute('select * from user')
result = cursor.fetchall()
print(result)
cursor.close()
conn.close()
if __name__ == '__main__':

func ()
Copy the code
PooledDB mode
to create a number of connections to the connection pool, shared by all threads.

Copy the code
import pymysql

Import PooledDB DBUtils.PooledDB from
the POOL = PooledDB (
Creator = pymysql, link database module #
maxconnections = 6, the maximum number of connections permitted by the connection pool #, and None 0 not to limit the number of connections
when mincached = 2, # initialization, the link free link to at least create the pool, 0 does not create
maxcached = 5, the largest pool of idle links # links, 0 and None not limit
maxshared = 3, the number of links up to a shared pool of links # 0 and None represent all share .PS: useless, because threadsafety pymysql and MySQLdb modules are 1, all values regardless of the setting for the number, _maxcached is always 0, so that all links are always shared.
blocking = True, # connection pool If no connection after, whether block waiting .True, wait; False, then do not wait for an error
maxusage = None, # a number of links up to be reused, None means unlimited
setsession = [], # command executed before the beginning of the session list.
the ping = 0,
# of ping the MySQL server, checks whether the service is available.
Host = '127.0.0.1',
Port = 3306,
User = 'the root',
password = '123456',
Database = 'TES T ',
charset =' UTF8 '
)


FUNC DEF ():
# detection is currently running number of connections is less than the maximum number of links, if not less then: wait or abnormal newspaper raise TooManyConnections
# or
links created when # is a priority to get the link initialization SteadyDBConnection.
# SteadyDBConnection then encapsulated into an object and returns PooledDedicatedDBConnection.
# If the link does not link the beginning of creation, the object is to create a SteadyDBConnection, repackaging and return to PooledDedicatedDBConnection.
# Once the link is closed, the connection is returned to the pool so that subsequent threads continue to be used.
conn = POOL.connection ()

# Print (th, 'link was taken away', conn1._con)
# print (th, 'There are pool', pool._idle_cache, '\ r \ n')

cursor = conn.cursor()
cursor.execute('select * from user')
result = cursor.fetchall()
print(result)
conn.close()

if __name__ == '__main__':

func()

Guess you like

Origin www.cnblogs.com/dingjiaoyang/p/11004949.html