GithubHelp home page GithubHelp logo

mozilla-github-standards / 364e8b3846e45490bb4aaea27f94702b82874352f607dd968046169efe5e09a9 Goto Github PK

View Code? Open in Web Editor NEW

This project forked from mozilla/django-post-request-task

0.0 0.0 0.0 28 KB

A celery task class whose execution is delayed until after the request finishes, using request_started and request_finished signals from django.

License: MIT License

Makefile 2.39% Python 97.61%

364e8b3846e45490bb4aaea27f94702b82874352f607dd968046169efe5e09a9's Introduction

django-post-request-task

https://travis-ci.org/mozilla/django-post-request-task.svg?branch=master

A celery 3.x task class whose execution is delayed until after the request finishes, using request_started and request_finished signals from django and thread locals.

This is useful if your views are wrapped in transactions (as they should if you're making database modifications in them), as you can end up triggering a celery task too soon before the transaction has been committed (or even trigger a task when the corresponding transaction has been rolled back).

By listening to the request_started and request_finished django signals, we can safely trigger a task after all transactions created from @atomic or ATOMIC_REQUESTS have been committed.

Usage

from celery import Celery
from post_request_task.task import PostRequestTask


app = Celery('myapp', task_cls=PostRequestTask)

@app.task
def my_task():
    # If .delay() is called on this task inside a django request-response
    # cycle it will be called once the request is finished, and not before.
    pass

Or, if you are using the task decorator directly:

from post_request_task.task import task

@task
def my_task():
    pass

That's it. If the task is called from outside the django request-response cycle, then it will be triggered normally.

As a bonus feature, if the same task is called with the same argument several times during a request-response cycle, it will only be queued up once.

Running tests

$ make testenv
$ make test

By default, tests are run with whatever django version is installed. If you want to run tests for other versions use tox:

$ make testenv
$ tox -e 2.7-1.11.x # or any other environment defined in our tox.ini

364e8b3846e45490bb4aaea27f94702b82874352f607dd968046169efe5e09a9's People

Contributors

diox avatar entequak avatar kseniyashaydurova avatar mozilla-github-standards 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.