Skip to main content
This is a pre-production deployment of Warehouse. Changes made here affect the production instance of PyPI (
Help us improve Python packaging - Donate today!

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

Project Description

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.


from celery import Celery
from post_request_task.task import PostRequestTask

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

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.

Or, if you are using the task decorator directly:

from post_request_task.task import task

def my_task():

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
Release History

Release History

This version
History Node


History Node


History Node


History Node


Download Files

Download Files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

File Name & Checksum SHA256 Checksum Help Version File Type Upload Date
django_post_request_task-0.2.0-py2-none-any.whl (7.9 kB) Copy SHA256 Checksum SHA256 2.7 Wheel Nov 7, 2017
django-post-request-task-0.2.0.tar.gz (6.1 kB) Copy SHA256 Checksum SHA256 Source Nov 7, 2017

Supported By

WebFaction WebFaction Technical Writing Elastic Elastic Search Pingdom Pingdom Monitoring Dyn Dyn DNS Sentry Sentry Error Logging CloudAMQP CloudAMQP RabbitMQ Heroku Heroku PaaS Kabu Creative Kabu Creative UX & Design Fastly Fastly CDN DigiCert DigiCert EV Certificate Rackspace Rackspace Cloud Servers DreamHost DreamHost Log Hosting