Skip to main content

Adds support for database query retries

Project description

PyPI version PyPI - Python Version GitHub stars PyPI - Downloads GitHub issues

Description

Django database retries. Main motivation for using this - to deal with network/db issues to avoid data-loss.

Usually when you develop a project locally with a single user - everything works perfectly. Local networks are super-stable, and simultaneous users don't bother you. When it comes to the real world - your application can (and definitely will) face network issues. Second case - deadlocks, which I personally see too often across different projects. And the only possible solution here to avoid the data-loss - to do a query retry.

IMPORTANT: Right now this works ONLY WITH MYSQL. If someone requires postgres/other dbs support - please create an issue

Usage

Install: pip install django-db-retry

Choose your flow (or use both):

  • Monkey-patch django internal methods (IMPORTANT: global patching won't handle retries for atomic transactions. with_query_retry decorator should be used instead)
  • Use explicit decorator

Monkey-patch:

Add next code somewhere on the top level of your project

from django_db_retry import install as install_db_retries
install_db_retries()

That's it =) All the needed underlying django methods will be wrapped with the retry decorator and will do execution retry if your app will face a network issue or deadlock

Decorator

Can be used on top of any function/view and will do a retry if deadlock/network error will happen. Default number of retries is 5. This value can be configured by using the QueryRetry class (see example 2):

from django_db_retry import with_query_retry
from django.db.transaction import atomic

@with_query_retry
def some_view():
    query_0, query_1 = ...
    with atomic():
        query_0()
        query_1()
    return ...

Configuring own value of retries

from django_db_retry import QueryRetry
from django.db.transaction import atomic

my_retry_decorator = QueryRetry(max_tries=100)

@my_retry_decorator
def some_view():
    query_0, query_1 = ...
    with atomic():
        query_0()
        query_1()
    return ...

Todo

  • Deal with atomic transactions during global patching
  • Add possibility to install the package using django INSTALLED_APPS settings

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-retry-0.1.2.tar.gz (5.5 kB view details)

Uploaded Source

File details

Details for the file django-db-retry-0.1.2.tar.gz.

File metadata

  • Download URL: django-db-retry-0.1.2.tar.gz
  • Upload date:
  • Size: 5.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.22.0 setuptools/42.0.1 requests-toolbelt/0.8.0 tqdm/4.27.0 CPython/3.7.0

File hashes

Hashes for django-db-retry-0.1.2.tar.gz
Algorithm Hash digest
SHA256 e273b6769addd556c95bd13f77798aed2f0d59d09843e4dc762d69106cf80bea
MD5 67b2adccee6b136271ac5b44894a389c
BLAKE2b-256 e904969567a4d42bac955d135f89f721d3fd69c2d71910652f03f40cccb746f1

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