I have a Flask-SQLAlchmey app running in Gunicorn connected to a PostgreSQL database, and I'm having trouble finding out what the pool_size
value should be and how many database connections I should expect.
This is my understanding of how things work:
- Processes in Python 3.7 DON'T share memory
- Each Gunicorn worker is it's own process
- Therefore, each Gunicorn worker will get it's own copy of the database connection pool and it won't be shared with any other worker
- Threads in Python DO share memory
- Therefore, any threads within a Gunicorn worker WILL share a database connection pool
Is that correct so far? If that is correct, then for a synchronous Flask app running in Gunicorn:
- Is the maximum number of database connections = (number of workers) * (number of threads per worker)?
- And within a worker, will it ever use more connections from a pool than there are workers?
Is there a reason why pool_size
should be larger than the number of threads? So, for a gunicorn app launched with gunicorn --workers=5 --threads=2 main:app
should pool_size
be 2? And if I am only using workers, and not using threads, is there any reason to have a pool_size
greater than 1?