Add a DB connection pool using gevent to django
Project description
Another DB pool using gevent for PostgreSQL DB.
Need Django 1.5.x or newer (check settings for django >= 1.6)
Python 3 is supported, but if gevent is not installed successfully it will use eventlet.
psycopg2
django-db-geventpool requires psycopg2:
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
Patch 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:
‘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)
If using django 1.6 or newer, 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 1.5.x
DATABASES = {
'default': {
'ENGINE': 'django_db_geventpool.backends.postgresql_psycopg2',
'NAME': 'db', # Or path to database file if using sqlite3.
'USER': 'postgres', # Not used with sqlite3.
'PASSWORD': 'postgres', # Not used with sqlite3.
'HOST': '', # Set to empty string for localhost. Not used with sqlite3.
'PORT': '', # Set to empty string for default. Not used with sqlite3.
'OPTIONS': {
'MAX_CONNS': 20
}
}
}
# for django 1.6 and newer version, CONN_MAX_AGE must be set to 0, or connections will never go back to the pool
DATABASES = {
'default': {
'ENGINE': 'django_db_geventpool.backends.postgresql_psycopg2',
'NAME': 'db',
'USER': 'postgres',
'PASSWORD': 'postgres',
'HOST': '',
'PORT': '',
'ATOMIC_REQUESTS': False,
'CONN_MAX_AGE': 0,
'OPTIONS': {
'MAX_CONNS': 20
}
}
}
Using Django 1.6+ ORM when not serving requests
If you are using django 1.6+ 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 have a 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 either sends the django.core.signals.request_finished signal or calls django.db.close_old_connections() right before it ends
The decorator method is preferred, but the other alternatives are also valid.
from django_db_geventpool.utils import close_connection
@close_connection
def greenlet_worker()
...
or
from django.core.signals import request_finished
def greenlet_worker():
...
request_finished.send(sender="greenlet")
or
from django.db import close_old_connections
def greenlet_worker():
...
close_old_connections()
Other pools
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for django-db-geventpool-3.2.0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | a8d8b18ff8621e3be8a9eecf8279b63b4ce6a0df3d3c3c5cbc096648e5a3ab7c |
|
MD5 | 8e4f49059041177fc1e21ea7917a3353 |
|
BLAKE2b-256 | e04c8695599bf8b9e8726ca76f85c979b88e6fb4d76aed8662672f7ea313072f |
Hashes for django_db_geventpool-3.2.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6d37fe0c1df3a3bfff13085e04c2fba6392be0e436e9c2ad941df95bb3e4caaa |
|
MD5 | d98322310a8bac50a2969299fb1afb96 |
|
BLAKE2b-256 | 32ed4a8b1701f32bf73dfc30982883851216750726326a248b86391b7c6f765e |