Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/jneight/django-db-geventpool
Another DB pool using gevent
https://github.com/jneight/django-db-geventpool
Last synced: 3 months ago
JSON representation
Another DB pool using gevent
- Host: GitHub
- URL: https://github.com/jneight/django-db-geventpool
- Owner: jneight
- License: apache-2.0
- Created: 2013-06-06T06:22:53.000Z (over 11 years ago)
- Default Branch: master
- Last Pushed: 2024-03-31T17:34:02.000Z (7 months ago)
- Last Synced: 2024-05-06T14:02:23.358Z (6 months ago)
- Language: Python
- Size: 112 KB
- Stars: 167
- Watchers: 5
- Forks: 29
- Open Issues: 6
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
- Authors: AUTHORS.rst
Awesome Lists containing this project
- starred-awesome - django-db-geventpool - Another DB pool using gevent (Python)
README
django-db-geventpool
====================[![CI](https://github.com/jneight/django-db-geventpool/actions/workflows/ci.yml/badge.svg)](https://github.com/jneight/django-db-geventpool/actions/workflows/ci.yml)
[![pypi version](https://img.shields.io/pypi/v/django-db-geventpool.svg)](https://pypi.python.org/pypi/django-db-geventpool)
[![pypi license](http://img.shields.io/pypi/l/django-db-geventpool.svg)](https://pypi.python.org/pypi/django-db-geventpool)
Another DB pool using gevent for PostgreSQL DB.
psycopg3
---------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]
```psycopg2
--------If **gevent** is not installed, the pool will use **eventlet** as fallback.
- `psycopg2>=2.5.1` for CPython 2 and 3 (or
[psycopg2-binary](https://pypi.org/project/psycopg2-binary/)---see
[notes in the psycopg2 2.7.4
release](http://initd.org/psycopg/articles/2018/02/08/psycopg-274-released/))
- `psycopg2cffi>=2.7` for PyPyPatch psycopg2
--------------Before using the pool, psycopg2 must be patched with psycogreen, if you
are using [gunicorn webserver](http://www.gunicorn.org/), a good place
is the
[post\_fork()](http://docs.gunicorn.org/en/latest/settings.html#post-fork)
function at the config file:``` {.python}
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 workersdef post_fork(server, worker):
patch_psycopg()
worker.log.info("Made Psycopg2 Green")
```Settings
--------> - 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``` {.python}
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
}
}
}
```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 endsThe decorator method with your function is preferred, but the other
alternatives are also valid``` {.python}
from django_db_geventpool.utils import close_connection@close_connection
def foo_func()
...
```or
``` {.python}
from django.core.signals import request_finisheddef foo_func():
...
request_finished.send(sender="greenlet")
```or
``` {.python}
from django.db import close_old_connectionsdef foo_func():
...
close_old_connections()
```Other pools
------------ [django-db-pool](https://github.com/gmcguire/django-db-pool)
- [django-postgresql](https://github.com/kennethreitz/django-postgrespool)