Python Sql Alchemy 连接超时
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3360951/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Sql Alchemy connection time Out
提问by Nazmul Hasan
I am using sqlalchemywith MySQL, and executing query with sql expression. When executing a number of query then it time out. I found an answerbut it is not clear to me. Please, any one can help me?
我正在使用sqlalchemywith MySQL,并使用 sql 表达式执行查询。当执行多个查询时,它会超时。我找到了答案,但我不清楚。拜托,有人可以帮我吗?
TimeoutError: QueuePool limit of size 5 overflow 10 reached, connection timed out, timeout 30
TimeoutError: QueuePool limit of size 5 overflow 10 达到,连接超时,超时 30
采纳答案by Greg
Whenever you create a new session in your code, make sure you close it. Just call session.close()
每当您在代码中创建新会话时,请确保将其关闭。打电话就行session.close()
When I got this error I thought I was closing all of my sessions, but I looked carefully and there was one new method where I wasn't. Closing the session in that method fixed this error for me.
当我收到此错误时,我以为我正在关闭所有会话,但我仔细查看了一种新方法,但我没有这样做。在该方法中关闭会话为我修复了这个错误。
回答by Aaren Shar
In multi-thread mode, if your concurrent request num is much more than the db connection pool size, it will throw the Queue Pool limit of size 5 overflow 10 reached error. try with this:
在多线程模式下,如果您的并发请求数量远大于数据库连接池大小,则会抛出队列池限制大小为 5 溢出 10 达到error。试试这个:
engine = create_engine('mysql://', convert_unicode=True,
pool_size=20, max_overflow=100)
to add the pool size
Add: the method above is not a correct way. The actual reason is that db connection pool is used up, and no other available connection. The most probably situation is you miss to release connection. For example:
补充:上面的方法不是正确的方法。实际原因是db连接池用完了,没有其他可用的连接。最有可能的情况是您错过了释放连接。例如:
@app.teardown_appcontext
def shutdown_session(exception=None):
db_session.remove()

