flask sqlalschemy应用程序中的超时问题:大小达到5的QueuePool溢出10,连接超时,超时30

问题描述 投票:0回答:1

嗨,当一些并发请求到达计算机时,我收到以下错误。

QueuePool limit of size 5 overflow 10 reached, connection timed out, timeout 30

我的怀疑是会话没有释放连接。

这是我如何创建会话和引擎的代码:

def create_db_engine():
    return create_engine(get_db_url(), strategy='threadlocal',
                     echo=get_config_by_config_name('SQLALCHEMY_ECHO'))


def get_session() -> Session:
"""Get a SQLAlchemy session object.
Returns:
  Instance of Session.

Note: use `sess.close()` after completing all transaction. Be a good citizen.
"""
    from app import engine
    Session = sessionmaker(bind=engine)
    conn = engine.connect()
    return Session(bind=conn)

我正在app.py中初始化引擎。

python sqlalchemy python-3.6 flask-sqlalchemy
1个回答
0
投票

您当然应该只有一位应用程序的会话制造者。我不是熟悉flask,但我想应该创建您的sessionmaker在您制造引擎的同一位置

app.engine
app.sessionmaker = sessionmaker(bind=app.engine)

然后您的get_session将是:

def get_session():
    return app.sessionmaker()   # <-- no bind here as the session maker is bound already

您还必须结束每个会话。我建议您阅读以下内容:SQLAlchemy - work with connections to DB and Sessions (not clear behavior and part in documentation)

© www.soinside.com 2019 - 2024. All rights reserved.