Add a DB connection pool using gevent to django
Low maintenance activity. Last release 21 months ago, commits in 0 of the last 26 weeks. Weaker on commit activity and recent releases.
pip install django-db-geventpooldjango-db-geventpool
Another DB pool using gevent for PostgreSQL DB.
psycopg3
Django, since 4.2, supports psycopg3. One of the advantages is that gevent is supported without needing extra patches, just install the package
$ pip install psycopg[binary]
psycopg2
If gevent is not installed, the pool will use eventlet as fallback.
-
psycopg2>=2.5.1for CPython 2 and 3 (or psycopg2-binary---see notes in the psycopg2 2.7.4 release) -
psycopg2cffi>=2.7for PyPyPatch psycopg2
Before using the pool, psycopg2 must be patched with psycogreen, if you are using gunicorn webserver, a good place is the post_fork() function at the config file:
from psycogreen.gevent import patch_psycopg # use this if you use gevent workers from psycogreen.eventlet import patch_psycopg # use this if you use eventlet workers def post_fork(server, worker): patch_psycopg() worker.log.info("Made Psycopg2 Green")
Settings
Set ENGINE in your database settings to:
- For psycopg3: 'django_db_geventpool.backends.postgresql_psycopg3' - For psycopg2: 'django_db_geventpool.backends.postgresql_psycopg2' - For postgis: 'django_db_geventpool.backends.postgis'Add MAX_CONNS to OPTIONS to set the maximun number of connections allowed to database (default=4)