我正在从主线程运行 python (v3.9.16) 应用程序,而单独的工作线程运行 asyncio 循环,对数据库进行 SQL 查询(使用 aioodbc v0.5.0)。目前有 4 个异步任务 在工作线程中运行。使用 create_async_engine 命令,我将连接池大小配置为 8,最大溢出配置为 10。
我添加了对监视连接池状态的支持,代码如下:
pool_status = session.get_bind().pool.status()
下面附有 pool_status 日志的片段,该片段在对数据库进行的每次查询期间显示。池大小为 8,考虑到我的 create_async_engine 配置,这是有意义的。 有人可以澄清 pool_status 其他三个组件的含义/行为:池中的连接、当前溢出、签出连接吗?例如,当前溢出令人困惑,因为它显示负值。
Pool size: 8 Connections in pool: 1 Current Overflow: -7 Current Checked out connections: 0
Pool size: 8 Connections in pool: 1 Current Overflow: -7 Current Checked out connections: 0
Pool size: 8 Connections in pool: 0 Current Overflow: -7 Current Checked out connections: 1
Pool size: 8 Connections in pool: 2 Current Overflow: -6 Current Checked out connections: 0
Pool size: 8 Connections in pool: 2 Current Overflow: -6 Current Checked out connections: 0
Pool size: 8 Connections in pool: 2 Current Overflow: -6 Current Checked out connections: 0
Pool size: 8 Connections in pool: 2 Current Overflow: -6 Current Checked out connections: 0
Pool size: 8 Connections in pool: 1 Current Overflow: -6 Current Checked out connections: 1
Pool size: 8 Connections in pool: 2 Current Overflow: -6 Current Checked out connections: 0
Pool size: 8 Connections in pool: 1 Current Overflow: -6 Current Checked out connections: 1
Pool size: 8 Connections in pool: 1 Current Overflow: -6 Current Checked out connections: 1
Pool size: 8 Connections in pool: 1 Current Overflow: -6 Current Checked out connections: 1
Pool size: 8 Connections in pool: 2 Current Overflow: -5 Current Checked out connections: 1
Pool size: 8 Connections in pool: 2 Current Overflow: -5 Current Checked out connections: 1
Pool size: 8 Connections in pool: 3 Current Overflow: -5 Current Checked out connections: 0
Pool size: 8 Connections in pool: 3 Current Overflow: -5 Current Checked out connections: 0
Pool size: 8 Connections in pool: 3 Current Overflow: -5 Current Checked out connections: 0
Pool size: 8 Connections in pool: 1 Current Overflow: -5 Current Checked out connections: 2
首先是简单的:
池大小:它指示池中可以建立的最大可用连接而不溢出。
Connections in pool:表示池中空闲(可用于新任务)的连接数。一旦使用它的任务完成,连接就会返回到池中。
当前检出连接数:表示池中任务当前正在使用的连接数。或无法用于新任务的连接
当前溢出:表示由于需求而从配置中额外建立的连接数(如果该数字为+ive)。
如果是否定的,可能有以下几个原因:
(a) 负值有时会出现在日志输出中,特别是在未正确返回连接,或者池大小和最大溢出之间存在配置错误的情况下。
修复它 - >
=> 确保所有连接在每次使用后都正确释放回池中。即使代码中出现异常,也应关闭连接或将连接返回到池中。
=> 确保
aioodbc
与池设置兼容,因为某些异步驱动程序在与 SQLAlchemy
中的某些池配置一起使用时可能会出现内部池管理冲突。