简体   繁体   English

Flask-SQLAlchemy 超时错误

[英]Flask-SQLAlchemy TimeoutError

My backend configuration is :我的后端配置是:

  • Ubuntu 12.04 Ubuntu 12.04
  • Python 2.7蟒蛇 2.7
  • Flask 0.9烧瓶 0.9
  • Flask-SQLAlchemy Flask-SQLAlchemy
  • Postgres 9.2 Postgres 9.2

I've got this error message:我收到此错误消息:

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

Do I need to close explicitly the db.session?我需要明确关闭 db.session 吗? Shouldn't be the connection back to pool when session goes out of scope?当会话超出范围时,不应该连接回池吗?

This could happen if you are using debug=True in your application and you have loaded several pages or API endpoints which errored out.如果您在应用程序中使用debug=True并且加载了多个出错的页面或 API 端点,则可能会发生这种情况。

The reason is that running the debug version of the app keeps a live debugger open in the error page.原因是运行应用程序的调试版本会在错误页面中保持实时调试器打开。 This live debugger keeps around all the resources from processing the request, so that you can examine them.这个实时调试器会阻止所有资源处理请求,以便您可以检查它们。 This means that the database connection can't be recycled.这意味着无法回收数据库连接。

You shouldn't use debug mode for the production version of your app (apart from problems like this it is a security risk), and the debugger often won't work anyway (it is designed to work with the flask test server, not with gunicorn).你不应该对你的应用程序的生产版本使用调试模式(除了这样的问题,这是一个安全风险),并且调试器通常不会工作(它被设计为与烧瓶测试服务器一起工作,而不是与枪炮)。 Therefore in prod the solution is to turn off debug.因此,在 prod 中,解决方案是关闭调试。

If you have this problem in dev using debug mode - this is a limitation.如果您在使用调试模式的开发中遇到此问题 - 这是一个限制。 You shouldn't be hitting the dev server so hard, or you can increase the limit.您不应该如此努力地访问开发服务器,或者您可以增加限制。 Be aware that 15 connections are usually plenty to serve a large number of concurrent requests when they are being recycled properly.请注意,当它们被正确回收时,15 个连接通常足以为大量并发请求提供服务。 It's only in debug that they tend to run out.只有在调试时,它们才会耗尽。

Flask-SQLAlchemy manages the connection pool for you, so in general, it should not be necessary to do this. Flask-SQLAlchemy 为您管理连接池,所以一般来说,应该没有必要这样做。 However, there are some instances in which it cannot control this, specifically if you are executing queries outside a request context or using with app.app_context() somewhere.但是,在某些情况下它无法控制这一点,特别是当您在请求上下文之外执行查询或with app.app_context()某处with app.app_context()使用with app.app_context()

When I paired Flask-SQLAlchemy with apscheduler , I found myself having to explicitly close sessions in the jobs the scheduler executed, or after several hours of running I would get this error.当我将 Flask-SQLAlchemy 与apscheduler配对时,我发现自己必须明确关闭调度程序执行的作业中的会话,或者在运行几个小时后我会得到这个错误。

I had to add the @app.teardown_request method, as well:我还必须添加@app.teardown_request方法:

@app.teardown_request
    def checkin_db(exc):
        user_store.db_session.remove()

I followed the "official" FlaskSecurity Basic SQLAlchemy Application with session quick start and after a couple of requests got the "sqlalchemy.exc.TimeoutError: QueuePool limit of size 5 overflow 10 reached, connection timed out, timeout 30 error".我按照“官方”FlaskSecurity Basic SQLAlchemy 应用程序进行了会话快速启动,在几次请求后得到了“sqlalchemy.exc.TimeoutError: QueuePool limit of size 5 overflow 10 reach, connection timed, timeout 30 error”。 Adding the above code seems to have fixed the issue:添加上面的代码似乎解决了这个问题:

  • FlaskSecurity: version 3.0.0 FlaskSecurity:版本 3.0.0
  • Flask: version 1.0.2烧瓶:版本 1.0.2
@app.teardown_request
def checkin_db(exc):
    try:
        g.db.close()
    except AttributeError:
        pass

Old af, but for anyone still having this issue, I was able to fix it by just restarting the Flask development server.旧的 af,但对于仍然存在此问题的任何人,我只需重新启动 Flask 开发服务器即可修复它。 Hope this helped!希望这有帮助!

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

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