Skip to main content

Add a DB connection pool using gevent to django

Project description

travis pypi version pypi license

Another DB pool using gevent for PostgreSQL DB.

Python 3 is supported, but if gevent is not installed successfully it will use fallback to eventlet.

psycopg2

django-db-geventpool requires psycopg2:

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)

  • 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

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,
            'REUSE_CONNS': 10
        }
    }
}

Using ORM when not serving requests

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()

Other pools

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django-db-geventpool-4.0.1.tar.gz (12.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_db_geventpool-4.0.1-py3-none-any.whl (13.4 kB view details)

Uploaded Python 3

File details

Details for the file django-db-geventpool-4.0.1.tar.gz.

File metadata

  • Download URL: django-db-geventpool-4.0.1.tar.gz
  • Upload date:
  • Size: 12.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.0 CPython/3.10.4

File hashes

Hashes for django-db-geventpool-4.0.1.tar.gz
Algorithm Hash digest
SHA256 14d45e70c00b4b89a6214fb2863735603c67e4f7e3e36b38d71c1bcb724af604
MD5 8abfde5c3f163718e772f1bedbfcfd82
BLAKE2b-256 ae7a2a293de750d389716dd0da4ce6ca83b47f1bd38bf73a06dc43da2bb46870

See more details on using hashes here.

File details

Details for the file django_db_geventpool-4.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for django_db_geventpool-4.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 8e7e10af412555739e25a427d6114a22134e8c5d0a1a90582c8187dd7bed491d
MD5 8f7accd4f23239ca219e56b92471b951
BLAKE2b-256 84e81a2551c90a3519d13591993e3067d207d8731868e222a7705dd855975e78

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page