Skip to main content

django_transaction_barrier - transaction barriers for Django and Celery

django_transaction_barrier provides a barrier-like abstraction for transactions. A Django application developer can use a “transaction barrier” to spawn a task within a transaction and guarantee that the task blocks until it’s able to access the updates made in the transaction. django_transaction_barrier is designed with Celery in mind and provides a Celery task base class. Using the base class it’s easy to write code that atomically modifies the database and spawns a Celery task that executes after the transaction commits.

Installation

Install from source or use pip:

pip install django_transaction_barrier

and add “django_transaction_barrier” to INSTALLED_APPS in settings.py:

INSTALLED_APPS = (
    'django_transaction_barrier',
    ...
)

Usage

from celery import task
from django.db import transaction
from django_transaction_barrier.celery import TransactionBarrierTask

@task(base=TransactionBarrierTask)
def do_something_task(model_id):
  value = Model.objects.get(id=model_id).value
  ...

@transaction.atomic
def kick_off_task(model, value):
    model.value = value
    do_something_task.apply_async_with_barrier(args=(model.id,))
    model.save()

Details

If an application spawns an asynchronous TransactionBarrierTask the task is guaranteed to execute eventually (assuming a durable task queue) after the transaction commits. If the transaction aborts, the task raises a TransactionAborted exception and does not execute. In autocommit mode (i.e., “outside of a transaction”) TransactionBarrierTasks behave like normal Celery tasks.

If an application synchrnously executes a TransactionBarrierTask (e.g., with Celery eager mode) within a transaction, the task executes immediately without waiting for the transaction to commit.

Implementation

django_transaction_barrier implements transaction barriers using row insertion to signify a committed transaction and some DB-specifc logic to detect an abort.

Tests

docker build -t tests . && docker run tests

TODO

  • Add support for a mysql backend.

Metadata

Release files for django-transaction-barrier 0.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-transaction-barrier 0.3
File Size Uploaded Provenance Actions
django-transaction-barrier-0.3.tar.gz 7.6 kB Without provenance Details for django-transaction-barrier-0.3.tar.gz

Release files / django-transaction-barrier-0.3.tar.gz

Download URL django-transaction-barrier-0.3.tar.gz
Size 7.6 kB
Tags Source
SHA-256 checksum
How to use checksums
6e4a476189a15f3361591c26ad316a803a840ce0b3ccd69a2d68a2c27b29e651
BLAKE2b-256 checksum
How to use checksums
996abd815d16a8937ee9995b9e53823600ce4a9c27e0e8a15a527ac49857812c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Security for django-transaction-barrier 0.3

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.3 This release

1 release file No provenance

0.2

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page