问题描述
我正在运行一个带有 Apscheduler 作业的 FastAPI 应用程序,该作业每 20 秒运行一次以从我们的数据库中进行轮询
app = FastAPI()
@app.on_event("startup")
async def startup():
from apscheduler.schedulers.asyncio import AsyncIOScheduler
scheduler = AsyncIOScheduler()
scheduler.add_job(check_database,"interval",seconds=20)
scheduler.start()
logger.info("scheduler started")
engine = create_engine(str(DATABASE_URL),pool_pre_ping=True,echo=True)
SessionLocal = sessionmaker(autocommit=False,autoflush=False,bind=engine)
def get_db():
db = SessionLocal()
try:
logger.info(engine.pool.status())
yield db
finally:
logger.info("close connection")
db.close()
def check_database():
session = next(get_db("check_external_sync"),None)
session.query(models.TestTable).all()
我不明白连接池的行为。我在日志中看到每次请求后都会调用“关闭连接”,为了简洁起见,我从下面的日志中隐藏了
- 为什么池以负溢出开始?
- 然后循环池大小为 5 - 0 并修复溢出,为什么池递减
- 最后一段时间后,池大小继续循环,但溢出开始增加,直到最终
sqlalchemy.exc.TimeoutError: QueuePool limit of size 5 overflow 10 reached
如果我们只使用 FastAPI,使用相同的 sessionmaker,池似乎表现得更明智,但我不明白使用 apscheduler。有人知道会发生什么吗?
[2021-07-10T12:20:20.636]Pool size: 5 Connections in pool: 0 Current Overflow: -5 Current Checked out connections: 0
[2021-07-10T12:20:40.635]Pool size: 5 Connections in pool: 1 Current Overflow: -4 Current Checked out connections: 0
[2021-07-10T12:21:00.631]Pool size: 5 Connections in pool: 0 Current Overflow: -4 Current Checked out connections: 1
[2021-07-10T12:21:20.632]Pool size: 5 Connections in pool: 0 Current Overflow: -3 Current Checked out connections: 2
[2021-07-10T12:21:40.632]Pool size: 5 Connections in pool: 0 Current Overflow: -2 Current Checked out connections: 3
[2021-07-10T12:22:00.631]Pool size: 5 Connections in pool: 0 Current Overflow: -1 Current Checked out connections: 4
[2021-07-10T12:22:20.632]Pool size: 5 Connections in pool: 0 Current Overflow: 0 Current Checked out connections: 5
[2021-07-10T13:10:40.642]Pool size: 5 Connections in pool: 5 Current Overflow: 0 Current Checked out connections: 0
[2021-07-10T13:11:00.633]Pool size: 5 Connections in pool: 4 Current Overflow: 0 Current Checked out connections: 1
[2021-07-10T13:11:20.632]Pool size: 5 Connections in pool: 3 Current Overflow: 0 Current Checked out connections: 2
[2021-07-10T13:11:40.631]Pool size: 5 Connections in pool: 2 Current Overflow: 0 Current Checked out connections: 3
[2021-07-10T13:12:00.632]Pool size: 5 Connections in pool: 1 Current Overflow: 0 Current Checked out connections: 4
[2021-07-10T13:12:20.631]Pool size: 5 Connections in pool: 0 Current Overflow: 0 Current Checked out connections: 5
[2021-07-10T13:12:40.643]Pool size: 5 Connections in pool: 5 Current Overflow: 0 Current Checked out connections: 0
[2021-07-10T13:13:00.632]Pool size: 5 Connections in pool: 4 Current Overflow: 0 Current Checked out connections: 1
[2021-07-10T13:13:20.632]Pool size: 5 Connections in pool: 3 Current Overflow: 0 Current Checked out connections: 2
[2021-07-10T13:13:40.632]Pool size: 5 Connections in pool: 2 Current Overflow: 0 Current Checked out connections: 3
[2021-07-10T13:14:00.632]Pool size: 5 Connections in pool: 1 Current Overflow: 0 Current Checked out connections: 4
[2021-07-10T13:14:20.633]Pool size: 5 Connections in pool: 0 Current Overflow: 0 Current Checked out connections: 5
[2021-07-10T13:14:40.641]Pool size: 5 Connections in pool: 5 Current Overflow: 0 Current Checked out connections: 0
[2021-07-10T13:18:40.633]Pool size: 5 Connections in pool: 5 Current Overflow: 0 Current Checked out connections: 0
[2021-07-10T13:19:00.634]Pool size: 5 Connections in pool: 5 Current Overflow: 0 Current Checked out connections: 0
[2021-07-10T13:19:20.632]Pool size: 5 Connections in pool: 4 Current Overflow: 0 Current Checked out connections: 1
[2021-07-10T13:19:40.632]Pool size: 5 Connections in pool: 3 Current Overflow: 0 Current Checked out connections: 2
[2021-07-10T13:20:00.631]Pool size: 5 Connections in pool: 2 Current Overflow: 0 Current Checked out connections: 3
[2021-07-10T13:20:20.633]Pool size: 5 Connections in pool: 1 Current Overflow: 0 Current Checked out connections: 4
[2021-07-10T13:20:40.632]Pool size: 5 Connections in pool: 0 Current Overflow: 0 Current Checked out connections: 5
[2021-07-10T13:21:00.631]Pool size: 5 Connections in pool: 5 Current Overflow: 1 Current Checked out connections: 1
[2021-07-10T13:21:20.632]Pool size: 5 Connections in pool: 4 Current Overflow: 1 Current Checked out connections: 2
[2021-07-10T13:21:40.631]Pool size: 5 Connections in pool: 3 Current Overflow: 1 Current Checked out connections: 3
[2021-07-10T13:22:00.632]Pool size: 5 Connections in pool: 2 Current Overflow: 1 Current Checked out connections: 4
[2021-07-10T13:22:20.632]Pool size: 5 Connections in pool: 1 Current Overflow: 1 Current Checked out connections: 5
[2021-07-10T13:22:40.632]Pool size: 5 Connections in pool: 0 Current Overflow: 1 Current Checked out connections: 6
[2021-07-10T13:23:00.632]Pool size: 5 Connections in pool: 5 Current Overflow: 2 Current Checked out connections: 2
[2021-07-10T13:23:20.631]Pool size: 5 Connections in pool: 4 Current Overflow: 2 Current Checked out connections: 3
[2021-07-10T13:23:40.632]Pool size: 5 Connections in pool: 3 Current Overflow: 2 Current Checked out connections: 4
[2021-07-10T13:24:00.633]Pool size: 5 Connections in pool: 2 Current Overflow: 2 Current Checked out connections: 5
[2021-07-10T13:24:20.633]Pool size: 5 Connections in pool: 1 Current Overflow: 2 Current Checked out connections: 6
[2021-07-10T13:24:40.632]Pool size: 5 Connections in pool: 0 Current Overflow: 2 Current Checked out connections: 7
[2021-07-10T13:25:00.633]Pool size: 5 Connections in pool: 5 Current Overflow: 3 Current Checked out connections: 3
[2021-07-10T13:25:20.632]Pool size: 5 Connections in pool: 4 Current Overflow: 3 Current Checked out connections: 4
[2021-07-10T13:25:40.632]Pool size: 5 Connections in pool: 3 Current Overflow: 3 Current Checked out connections: 5
[2021-07-10T13:26:00.632]Pool size: 5 Connections in pool: 2 Current Overflow: 3 Current Checked out connections: 6
[2021-07-10T13:26:20.632]Pool size: 5 Connections in pool: 1 Current Overflow: 3 Current Checked out connections: 7
[2021-07-10T13:26:40.628]Pool size: 5 Connections in pool: 0 Current Overflow: 3 Current Checked out connections: 8
[2021-07-10T13:26:40.670]Pool size: 5 Connections in pool: 5 Current Overflow: 3 Current Checked out connections: 3
[2021-07-10T13:27:00.633]Pool size: 5 Connections in pool: 5 Current Overflow: 3 Current Checked out connections: 3
[2021-07-10T13:27:20.633]Pool size: 5 Connections in pool: 4 Current Overflow: 3 Current Checked out connections: 4
[2021-07-10T13:27:40.631]Pool size: 5 Connections in pool: 3 Current Overflow: 3 Current Checked out connections: 5
[2021-07-10T13:28:00.633]Pool size: 5 Connections in pool: 2 Current Overflow: 3 Current Checked out connections: 6
[2021-07-10T13:28:20.632]Pool size: 5 Connections in pool: 1 Current Overflow: 3 Current Checked out connections: 7
[2021-07-10T13:28:40.633]Pool size: 5 Connections in pool: 0 Current Overflow: 3 Current Checked out connections: 8
[2021-07-10T13:29:00.633]Pool size: 5 Connections in pool: 5 Current Overflow: 4 Current Checked out connections: 4
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)