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

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

Project details


Download files

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

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

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

Uploaded Python 2 Python 3

File details

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

File metadata

  • Download URL: django_post_request_task-0.4.1-py3-none-any.whl
  • Upload date:
  • Size: 7.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.10.0 pkginfo/1.2.1 requests/2.18.4 setuptools/39.0.1 requests-toolbelt/0.8.0 tqdm/4.19.5 CPython/3.6.8

File hashes

Hashes for django_post_request_task-0.4.1-py3-none-any.whl
Algorithm Hash digest
SHA256 c8be3e96d3a58616ec750f3a2d3add62eab8adf44c572c12de89130408917eac
MD5 d9f6c59b2413a99abbf8666395174b8d
BLAKE2b-256 596543886c2b0073eb00ff044624252a3d66cf09734763117440160d66b764aa

See more details on using hashes here.

File details

Details for the file django_post_request_task-0.4.1-py2.py3-none-any.whl.

File metadata

  • Download URL: django_post_request_task-0.4.1-py2.py3-none-any.whl
  • Upload date:
  • Size: 7.9 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.10.0 pkginfo/1.2.1 requests/2.18.4 setuptools/39.0.1 requests-toolbelt/0.8.0 tqdm/4.19.5 CPython/3.6.8

File hashes

Hashes for django_post_request_task-0.4.1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 19b4d22b02abe74ea48946aaf74d8d698e46a4911e2271059420238580420358
MD5 04713df5766ef286ee2b97a1bb515d90
BLAKE2b-256 04f7ea992bc7cffd314bcc23c7bddcc4bc7bd31bf9751dd32e1ccaefa47ab8c5

See more details on using hashes here.

File details

Details for the file django_post_request_task-0.4.1-py2-none-any.whl.

File metadata

  • Download URL: django_post_request_task-0.4.1-py2-none-any.whl
  • Upload date:
  • Size: 7.8 kB
  • Tags: Python 2
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.10.0 pkginfo/1.2.1 requests/2.18.4 setuptools/39.0.1 requests-toolbelt/0.8.0 tqdm/4.19.5 CPython/3.6.8

File hashes

Hashes for django_post_request_task-0.4.1-py2-none-any.whl
Algorithm Hash digest
SHA256 9ae8f3c7255d00717a4699cd39377c42c47bcc2524306d2d4ecf80f6c6b1f6b1
MD5 7c1929c27d2971ce1af2decaf6a4419b
BLAKE2b-256 ce2aedd690e7a750959e146a999ea96486c184c696304738b0c0339d5e2f9342

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