
Security News
Open Source CAI Framework Handles Pen Testing Tasks up to 3,600× Faster Than Humans
CAI is a new open source AI framework that automates penetration testing tasks like scanning and exploitation up to 3,600× faster than humans.
django-db-geventpool
Advanced tools
Another DB pool using gevent for PostgreSQL DB.
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]
If gevent is not installed, the pool will use eventlet as fallback.
psycopg2>=2.5.1
for CPython 2 and 3 (or
psycopg2-binary---see
notes in the psycopg2 2.7.4
release)
psycopg2cffi>=2.7
for PyPy
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")
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)
Add REUSE_CONNS to OPTIONS to indicate how many of the MAX_CONNS should be reused by new requests. Will fallback to the same value as MAX_CONNS if not defined
Add 'CONN_MAX_AGE': 0 to settings to disable default django persistent connection feature. And read below note if you are manually spawning greenlets
For Django < 5.1:
DATABASES = {
'default': {
'ENGINE': 'django_db_geventpool.backends.postgresql_psycopg',
'NAME': 'db',
'USER': 'postgres',
'PASSWORD': 'postgres',
'HOST': '',
'PORT': '',
'ATOMIC_REQUESTS': False,
'CONN_MAX_AGE': 0,
'OPTIONS': {
'MAX_CONNS': 20,
'REUSE_CONNS': 10
}
}
}
For Django >= 5.1, native pool support should be disabled:
DATABASES = {
'default': {
'ENGINE': 'django_db_geventpool.backends.postgresql_psycopg',
'NAME': 'db',
'USER': 'postgres',
'PASSWORD': 'postgres',
'HOST': '',
'PORT': '',
'ATOMIC_REQUESTS': False,
'CONN_MAX_AGE': 0,
'OPTIONS': {
'MAX_CONNS': 20,
'REUSE_CONNS': 10.
'pool': False,
}
}
}
If you are using django with celery (or other), or have code that manually spawn greenlets it will not be sufficient to set CONN_MAX_AGE to 0. Django only checks for long-live connections when finishing a request - So if you manually spawn a greenlet (or task spawning one) its connections will not get cleaned up and will live until timeout. In production this can cause quite some open connections and while developing it can hamper your tests cases.
To solve it make sure that each greenlet function (or task) either sends the django.core.signals.request_finished signal or calls django.db.close_old_connections() right before it ends
The decorator method with your function is preferred, but the other alternatives are also valid
from django_db_geventpool.utils import close_connection
@close_connection
def foo_func()
...
or
from django.core.signals import request_finished
def foo_func():
...
request_finished.send(sender="greenlet")
or
from django.db import close_old_connections
def foo_func():
...
close_old_connections()
FAQs
Add a DB connection pool using gevent to django
We found that django-db-geventpool demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
CAI is a new open source AI framework that automates penetration testing tasks like scanning and exploitation up to 3,600× faster than humans.
Security News
Deno 2.4 brings back bundling, improves dependency updates and telemetry, and makes the runtime more practical for real-world JavaScript projects.
Security News
CVEForecast.org uses machine learning to project a record-breaking surge in vulnerability disclosures in 2025.