The pgdbpool Python Module is a tiny PostgreSQL Database Connection De-Multiplexer primarily scoped for Web- / Application Server.
+----------------------+ +--------------- - - -
| WebServer Service.py | -- Handler Con #1 ----> | PostgreSQL
| Request / Thread #1 | | Backend
+----------------------+ |
|
+----------------------+ |
| WebServer Service.py | -- Handler Con #2 ----> |
| Request / Thread #2 | |
+----------------------+ +--------------- - - -
If configured in a Web-Servers WSGI Python Script, the Pooling-Logic is quite simple.
- Check if a free connection in the pool exists
- Check if connection usable (SQL ping)
- Use connection and protect it from beeing used until querie(s) finished
- Release connection for usage again
- Try reconnecting to endpoint if connection has been lost
Currently Thread Safety is guaranteed by lock = threading.Lock()
which implies a Kernel Mutex syscall().
The concept works, but the GIL (Python Global Interpreter Lock) thwarts our plans 😞.
In detail: if used in a threaded Web-Server setup, it does not really scale well on heavy loads.
Important
Take a closer look at "6. Future", problem solved probably.
Python 3 and psycopg2 module is required.
# apt-get install python3-psycopg2
# pip install pgdbpool
See documentation either at ./doc or https://pythondocs.webcodex.de/pgdbpool/ for detailed explanation / illustrative examples.
DB-Pooling also should be usable in FalconAS Python Application Server (https://github.com/WEBcodeX1/http-1.2).
The model here: 1 Process == 1 Python Interpreter (threading-less), GIL Problem solved.
Note
Also a Pool should be configurable to use multiple (read-loadbalanced) PostgreSQL Endpoints.