MariaDBConnector/Python连接池获取池中每个连接的连接速度变慢

kknvjkwl  于 2023-02-04  发布在  Python
关注(0)|答案(1)|浏览(175)

我在Python 3.11、Linux Mint、最新版本的MariaDB等平台上使用MariaDB/Python连接器,但我一辈子都搞不清楚这个问题。我有一个简单的函数get_connection(),它从mariadb.ConnectionPool返回Connection,使用pool_size=1时,Postman的响应时间是75 ms,但使用pool_size=5时,响应时间为400 ms。除了打开和关闭此连接的FastAPI中间件之外,此请求中没有其他内容。此问题存在一个阈值,即每个请求打开一个新连接的速度比使用ConnectionPool更快。
我希望get_connection()具有相同的响应时间,无论我使用的是pool_size=5还是pool_size=64
下面是代码:

POOL = mariadb.ConnectionPool(
    host=os.environ.get("DATABASE_IP"),
    port=3306,
    user=os.environ.get("DATABASE_USER"),
    password=os.environ.get("DATABASE_PASSWORD"),
    pool_name="poolname",
    pool_size=5,
    pool_reset_connection=True
)

def get_connection() -> Connection:
    """
    Returns a DatabaseHandler object
    :return:
    """
    before = datetime.datetime.now()
    connection = POOL.get_connection()
    after = datetime.datetime.now()
    print((after - before).microseconds)
    return connection
tkqqtvp1

tkqqtvp11#

我们在使用连接池而不是单个连接运行internal benchmarks时遇到了同样的问题。
此问题是由于在对未使用的连接进行迭代时进行不必要的健康检查(发送MYSQL_PING)而导致的。
计划在1.1.6中修复(计划在2月中旬)
有关此问题的跟踪状态,请检查CONPY-245
更新(2月1日):问题已修复,将在1.1.6版本中提供:

$ python3.11 conpy245.py
Acquiring 100000 connections via Pool
Pool size: 64
Average time per connection: 19 ms

Acquiring 100000 connections without Pool
Average time per connection: 170 ms

相关问题