Skip to main content

Django Database Connection Pooling with Gevent workers

Project description

django_db_pooling

A patch to enable Django database connection pooling with gevent Gunicorn workers.

Usage Guide

  1. pip install django_db_pooling

  2. in wsgi.py:

     import os
     import pymysql
     from django.core.wsgi import get_wsgi_application
     from django_db_pooling import pooling
    
     pymysql.install_as_MySQLdb()
    
     os.environ.setdefault("DJANGO_SETTINGS_MODULE", "xxxxx.settings")
    
     application = get_wsgi_application()
    
     pooling.set_pool_size(4)
     pooling.apply_patch()
    
  3. set conn_max_age to a value larger than 0 and less than MySQL's wait_timeout, e.g 60 seconds is usually a good enough value.

How it works

The connection pool patches the __getitem__ method of
django.db.utils.ConnectionHandler in such a way so that the db connection can be reused across multiple requests/green-lets until conn_max_age is reached.

By default, Django manage the database connection object in a thread local attributed _connections in django.db.utils.ConnectionHandler so that each thread has its own database connection object to avoid race condition. It also allows reuse of database connection by specifying a positive value or None on conn_max_age setting to avoid initiating and releasing connection for each request. However, it leads to problem when used with the gevent worker in Gunicorn where the thread local attribute is patched by a green-let local attribute: if a green-let is not reused, the database connection object associated with green-let will not be released or recycled until it is garbage collected or closed by MySQL. Under a medium or heavy load, those idle connections may accumulate and the connection limit of MySQL is finally reached so that no more new connection can be established.

To solve the above issue, this patch adds a non-local attribute 'connection_pool'. It is a dictionary with keys being the connection alias name and values being a list of pooled connection objects. Whenever a new request starts, connection object in the pool will be reused if there is any or else new connection object is created. The connection object will be bound to the _connections attribute to associate with the current request. Underlying connection is closed and reopened if the conn_max_age is reached. When current request finishes, the connection object will be recycled to the pool if pool still has capacity, which is set by the method set_pool_size()(default to 1 if not specified). Otherwise the connection object will be freed and the underlying connection is closed immediately.

Under extrem heavy load , it might be a good idea to restrict the concurrency per worker in Gunicorn (--worker_connections) to ensure the overall concurrent database connections are less than the maximum permitted simultaneous connections in MySQL (--max_connections).

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_pooling-0.0.2.tar.gz (3.2 kB view details)

Uploaded Source

Built Distribution

django_db_pooling-0.0.2-py3-none-any.whl (3.6 kB view details)

Uploaded Python 3

File details

Details for the file django_db_pooling-0.0.2.tar.gz.

File metadata

  • Download URL: django_db_pooling-0.0.2.tar.gz
  • Upload date:
  • Size: 3.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.19.1 setuptools/40.4.3 requests-toolbelt/0.8.0 tqdm/4.26.0 CPython/3.6.6

File hashes

Hashes for django_db_pooling-0.0.2.tar.gz
Algorithm Hash digest
SHA256 15e06a6aed5a8dc3a57d1d7a397725293b1e2dd2f39d56d2193099842a7651d1
MD5 40807d3d49187cbb6243ca7fafdcd87a
BLAKE2b-256 201689bdea22c773ffea33128d42701eab64401d8f72288f69693f925a36b451

See more details on using hashes here.

File details

Details for the file django_db_pooling-0.0.2-py3-none-any.whl.

File metadata

  • Download URL: django_db_pooling-0.0.2-py3-none-any.whl
  • Upload date:
  • Size: 3.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.19.1 setuptools/40.4.3 requests-toolbelt/0.8.0 tqdm/4.26.0 CPython/3.6.6

File hashes

Hashes for django_db_pooling-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 31044783e0b2dd0657d7e573dca4d82012109536b6fdb57ae3c27c1e62c24b3d
MD5 f77dd557b97d2b3c6cada342ae1e5818
BLAKE2b-256 93c43fbb4ffe4355a6046a6ce1d0d862f3b4646ff672dd751e08abe6c990f3cc

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