Massimo Fazzolari Massimo Fazzolari - 3 months ago 26
Python Question

Flask-SQLAlchemy TimeoutError

My backend configuration is :

  • Ubuntu 12.04

  • Python 2.7

  • Flask 0.9

  • Flask-SQLAlchemy

  • 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? Shouldn't be the connection back to pool when session goes out of scope?


Flask-SQLAlchemy manages the connection pool for you, so in general, it should not be necessary to do this. 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.

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.