简体   繁体   English

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

[英]MariaDBConnector/Python Connection Pools get slower to get connections with each connection in the pool

I'm using the MariaDB/Python connector on Python 3.11, Linux Mint, latest version of MariaDB, etc and I can't for the life of me figure this out.我在 Python 3.11、Linux Mint、最新版本的 MariaDB 等上使用 MariaDB/Python 连接器,我这辈子都弄不明白。 I have a simple function called get_connection() that returns a Connection from mariadb.ConnectionPool , and with pool_size=1 , the response time in Postman is 75ms, but with pool_size=5 , the response time is 400ms.我有一个名为get_connection()的简单 function,它从mariadb.ConnectionPool返回一个Connection ,并且pool_size=1时,Postman 中的响应时间为 75 毫秒,而pool_size=5时,响应时间为 400 毫秒。 There is nothing else going on in this request except for a FastAPI Middleware that opens and closes this connection.除了打开和关闭此连接的 FastAPI 中间件外,此请求中没有其他任何内容。 There comes a threshold with this issue where it's simply faster to open a new connection per request, as opposed to using a ConnectionPool .这个问题有一个门槛,与使用ConnectionPool相比,每个请求打开一个新连接更快。

I expect get_connection() to have the same response time whether I have pool_size=5 or pool_size=64 .我希望get_connection()具有相同的响应时间,无论我有pool_size=5还是pool_size=64

Here is the code:这是代码:

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

We experienced the same problem when running internal benchmarks with connection pool instead of a single connection.在使用连接池而不是单个连接运行内部基准测试时,我们遇到了同样的问题。

The problem is caused by unnecessary health check (sending MYSQL_PING) when iterating over unused connections.问题是在迭代未使用的连接时由不必要的健康检查(发送 MYSQL_PING)引起的。

Fix is planned for 1.1.6 (scheduled for mid Feb)计划在 1.1.6 中修复(计划于 2 月中旬)

For tracking status of this issue, please check CONPY-245有关此问题的跟踪状态,请查看CONPY-245

Update (Feb 1): Issue has been fixed and will be available in 1.1.6 release:更新(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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM