GithubHelp home page GithubHelp logo

pavlenko-volodymyr / django-celery-transactions Goto Github PK

View Code? Open in Web Editor NEW

This project forked from bradleyayers/django-celery-transactions

0.0 3.0 0.0 77 KB

Django transaction support for Celery tasks.

License: Other

django-celery-transactions's Introduction

django-celery-transactions

django-celery-transactions holds on to Celery tasks until the current database transaction is committed, avoiding potential race conditions as described in Celery's user guide. Send tasks from signal handlers without fear!

Features

  • If the transaction is rolled back, the tasks are discarded. Django's transaction middleware does this if an exception is raised.
  • If transactions aren't being managed, tasks are sent as normal. This means that sending tasks from within Django's shell will work as expected, as will the various transaction decorators commit_manually, commit_on_success, etc.

Installation & Use

  1. Install django-celery-transactions from PyPI:

     $ pip install django-celery-transactions
    
  2. Use the patched decorator to create your tasks:

     from djcelery_transactions import task
     from models import Model
    
    
     @task
     def print_model(model_pk):
         print Model.objects.get(pk=model_pk)
    
  3. Then use them as normal:

     from django.db import transaction
     from models import Model
     from tasks import print_model
    
    
     # This task will be sent after the transaction is committed. This works
     # from anywhere in the managed transaction block (e.g. signal handlers).
     def view(request):
         model = Model.objects.create(...)
         print_model.delay(model.pk)
    
    
     # This task will not be sent (it is discarded) as the transaction
     # middleware rolls the transaction back when it catches the exception.
     def failing_view(request, model_pk):
         print_model.delay(model_pk)
         raise Exception()
    
    
     # This task will be sent immediately as transactions aren't being
     # managed and it is assumed that the author knows what they're doing.
     @transaction.commit_manually
     def manual_view(request, model_pk):
         print_model.delay(model_pk)
         transaction.commit()
    

Run test suite

    $ python setup.py test

django-celery-transactions's People

Contributors

bradleyayers avatar chrisdoble avatar marcinn avatar nicolas-dh avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.