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