Package: pooling
poolingpooling anything.Installpip install poolingUsage Example 1import MySQLdb
from MySQLdb.cursors import DictCursor

from pooling import PoolBase

class MysqlConnectionPool(PoolBase):
    
    def do_session_create(self, *create_args, **create_kwargs):
        create_kwargs.setdefault("cursorclass", DictCursor)
        create_kwargs.setdefault("autocommit", True)
        create_kwargs.setdefault("charset", "utf8mb4")
        return MySQLdb.connect(*create_args, **create_kwargs)

    def do_session_destory(self, real_session):
        real_session.close()

if __name__ == "__main__":
    conn_info = {
        "host": "127.0.0.1",
        "port": 3306,
        "user": "test",
        "password": "test",
        "db": "test",
    }
    pool = MysqlConnectionPool(pool_size=10, kwargs=conn_info)
    connection = pool.get_session()
    cursor = connection.cursor()
    count = cursor.execute("show databases")
    data = cursor.fetchall()
    print("rows count=", count)
    print("data=", data)
    assert count == len(data)Usage Example 2import MySQLdb
from MySQLdb.cursors import DictCursor

from pooling import Pool

def mysql_conn_create():
    conn_info = {
        "host": "127.0.0.1",
        "port": 3306,
        "user": "test",
        "password": "test",
        "db": "test",
    }
    conn = MySQLdb.connect(cursorclass=DictCursor, autocommit=True, **conn_info)
    return conn

def mysql_conn_close(session):
    session.close()

if __name__ == "__main__":
    pool = Pool(pool_size=10, create_factory=mysql_conn_create, destory_factory=mysql_conn_close)
    connection = pool.get_session()
    cursor = connection.cursor()
    count = cursor.execute("show databases")
    data = cursor.fetchall()
    print("rows count=", count)
    print("data=", data)
    assert count == len(data)Usage Example 3conn_info = {
    "host": "127.0.0.1",
    "port": 3306,
    "user": "test",
    "password": "test",
    "db": "test",
}

from pooling.mysql import MysqlConnectionPool

pool = MysqlConnectionPool(10, kwargs=conn_info)
connection = pool.get_session()
cursor = connection.cursor()
count = cursor.execute("show databases")
data = cursor.fetchall()
print("rows count=", count)
print("data=", data)
assert count == len(data)Usage Example 4conn_info = {
    "host": "127.0.0.1",
    "port": 3306,
    "user": "test",
    "password": "test",
    "db": "test",
}

from pooling.mysql import MysqlConnectionPool

pool = MysqlConnectionPool(10, kwargs=conn_info)
data = pool.query("select * from table1")
for row in data:
    print(row)NoteCall pool.get_session() returns a proxied connection instance.The returned proxied connection instance is a proxy instance of the real connection.When the returned proxied connection is being deleted, the real connection will be returned to the pool so that the real connection can be used again.A simple mysql connection pool can be imported by doingfrom pooling.mysql import MysqlConnectionPool. Compare with theMysqlConnectionPoolimplemented inUsage Example 1, it add ping() test and errors handler in get_session() method.Thepooling.mysqlmodule depends onmysqlclient, butpoolingis NOT, so thatmysqlclientis not auto installed afterpoolinginstalled. You should dopip install mysqlclientby yourself if you want to useMysqlConnectionPool.If you want to useMysqlConnectorPool, you need to install packagemysql-connector-pythonby yourself.If you want to usePyMySQLConnectionPool, you need to install packagePyMySQLby yourself.Releasesv0.1.0First release.v0.1.1MysqlConnectionPool.get_session()doconnection.ping()to make sure the connection is avaiable.Pool.counterandPool.versionuse thread safe counter.v0.1.2Fix problem in usingget_sessionwithwithstatement.with pool.get_session() as session: passgot the real session instread of our wrapped Session.v0.1.5Fix problem in handling a proxied real-session instance.Fix un-sleep-problem while connecting to mysql server failed.Add python2.7 support.v0.1.6Use zenutils.importutils.Fix get_session timeout calc problem.v0.1.7Unit test passed.v0.1.8Add _pooling_is_connection_closed test.v0.1.9Fix doing useless time.sleep problem in pooling.mysql.MysqlConnectionPoolBase.get_session.Add pooling.mysql.PyMySQLConnectionPool.v0.1.10Fix PoolBase.return_session double called inwith pool.get_session() as session: pass.PoolBase add ping_test support.v0.1.11Doc update.