Skip to main content

A celery task class whose execution is delayed until after the request finishes

Project description

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

Tests are run with Django 1.8.x by default. If you want to run tests for other versions use tox:

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

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-post-request-task-0.1.1.tar.gz (6.1 kB view details)

Uploaded Source

Built Distribution

django_post_request_task-0.1.1-py3-none-any.whl (7.9 kB view details)

Uploaded Python 3

File details

Details for the file django-post-request-task-0.1.1.tar.gz.

File metadata

File hashes

Hashes for django-post-request-task-0.1.1.tar.gz
Algorithm Hash digest
SHA256 015b407b8bc81f13104a5607c2765cc2cafc628661ffcfa1f4563fdc9eba2ffb
MD5 06941a65e0b825a0a85e79e787eff07d
BLAKE2b-256 679e0e9adad5ce9a552b93a83b0fd709f2fad91fb4d206a636727fe3d715d5d9

See more details on using hashes here.

File details

Details for the file django_post_request_task-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for django_post_request_task-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 f5b0d532a625321d7613bf6c7b6ba7e6c4c5e77e257d780db414799f62786d63
MD5 603016de6024069c8f835f727e37aadf
BLAKE2b-256 c7c7012678f7359cb3575f18f9fd9e66c5fa8c251e56465cc9bf1ea7634ad9a9

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