Skip to main content

Gevent-Friendly Database Connection Pooling in Django

Project description

Thanks to SQLAlchemy, we can pool the database connections while using the Gevent’s monkey patching.

It supports MySQL and has been tested under Python 2.7, Django 1.11, Gevent 1.2, and SQLAlchemy 1.2.

Besides, DjangoQueuePool is a new queue pool extending the SQLAlchemy’s QueuePool:

  • Reuse the database connections overflowed by burst traffic;

  • Retire the unused database connections gradually over time.

Please remember to close the unusable or obsolete database connections:

  • The closed connections are returned to the pool;

  • It is recommended to close them once a task is done.

  • It is recommended to set CONN_MAX_AGE as 0 if possible;

  • The connections would always be obsolete if CONN_MAX_AGE is 0.

from django.db import connections

for conn in connections.all():
    conn.close_if_unusable_or_obsolete()

Getting Started

  • Install the database connection pool

pip install django-db-conn-pool
  • Add the pool to the Django database backend

DATABASES = {
    'default': {
        'ENGINE': 'django_db_conn_pool.mysqlalchemy',
        'CONN_MAX_AGE': 0,
        'POOL': db_conn_pool,
        ...
    }
}
  • Select and tune the connection pool parameters

from sqlalchemy.pool import QueuePool
from django_db_conn_pool.mysqlalchemy.pool import DjangoQueuePool


db_conn_pool = slow_and_safe = {

    'django_pool_class': QueuePool,         # sqlalchemy's builtin queue pool class
    'django_pre_ping': True,                # pre ping by django if dialect is None
    'django_reset_on_return': False,        # use sqlalchemy's reset on conn return

    'pool_size': 5,                         # daily traffic: reuse long connections
    'max_overflow': 0,                      # burst traffic: do not overload the db
    'timeout': 30,                          # burst traffic: > external api timeout
    'recycle': 120,                         # should be smaller than mysql timeout
    'dialect': None,                        # sqlalchemy's mysql dialect instance
    'pre_ping': False,                      # sqlalchemy pre ping requires dialect
    'use_threadlocal': True,                # every thread always get its same conn
    'reset_on_return': 'rollback',          # reset on every conn return by rollback
}

db_conn_pool = fast_and_sane = {

    'django_pool_class': QueuePool,         # sqlalchemy's builtin queue pool class
    'django_pre_ping': False,               # no pre ping due to long mysql timeout
    'django_reset_on_return': True,         # reset by rollback only when necessary

    'pool_size': 5,                         # daily traffic: reuse long connections
    'max_overflow': 10,                     # burst traffic: do not overload the db
    'timeout': 30,                          # burst traffic: > external api timeout
    'recycle': 3600,                        # to be much smaller than mysql timeout
    'dialect': None,                        # sqlalchemy's mysql dialect instance
    'pre_ping': False,                      # sqlalchemy pre ping requires dialect
    'use_threadlocal': False,               # diff threads share the db connections
    'reset_on_return': None,                # do not use sqlalchemy reset on return
}

db_conn_pool = fast_and_wild = {

    'django_pool_class': DjangoQueuePool,   # customized from sqlalchemy queue pool
    'django_pre_ping': False,               # no pre ping due to long mysql timeout
    'django_reset_on_return': True,         # reset by rollback only when necessary
    'django_core_pool_size': 5,             # retire no conn if achieving core size
    'django_unload_timeout': 2,             # wait some random time before overload
    'django_retire_interval': 5,            # retire few non-core conn per interval
    'django_retire_quantity': 1,            # retire few non-core conn per interval

    'pool_size': 30,                        # daily traffic: recycle or retire conn
    'max_overflow': 0,                      # burst traffic: put overflow into pool
    'timeout': 30,                          # burst traffic: > external api timeout
    'recycle': 3600,                        # to be much smaller than mysql timeout
    'dialect': None,                        # sqlalchemy's mysql dialect instance
    'pre_ping': False,                      # sqlalchemy pre ping requires dialect
    'use_threadlocal': False,               # diff threads share the db connections
    'reset_on_return': None,                # do not use sqlalchemy reset on return
}

Release History

1.0.0 (2019-05-04)

  • Birthday

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-conn-pool-1.0.0.tar.gz (10.6 kB view details)

Uploaded Source

Built Distribution

django_db_conn_pool-1.0.0-py3-none-any.whl (11.4 kB view details)

Uploaded Python 3

File details

Details for the file django-db-conn-pool-1.0.0.tar.gz.

File metadata

  • Download URL: django-db-conn-pool-1.0.0.tar.gz
  • Upload date:
  • Size: 10.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.3

File hashes

Hashes for django-db-conn-pool-1.0.0.tar.gz
Algorithm Hash digest
SHA256 8bc872847e1c1a74ec6b49203247bed798e8c0ed2864caf7a28b5760feca81e3
MD5 b77c6b6c1248f605c20a356173ad7a1c
BLAKE2b-256 d054dbc861ad54e3600c8a9881e5c34dd26d54ea72c7171e5c5e48e170dab5b4

See more details on using hashes here.

File details

Details for the file django_db_conn_pool-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: django_db_conn_pool-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 11.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.3

File hashes

Hashes for django_db_conn_pool-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 0ddc5233531eb8639f413e150f8e80931d7aad1a99bcd11ad98bc45274bc2df2
MD5 db9adbb8e7c7211b55694ae1a4a57155
BLAKE2b-256 370458a2a6ce9d3410643e1f719cfef60bd93a988009c856b0cd2f5d085aa420

See more details on using hashes here.

Supported by

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