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.
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 override this, use:
$ DJANGO="Django==1.9" make testenv
$ make test
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
Built Distribution
File details
Details for the file django_post_request_task-0.0.2-py2-none-any.whl
.
File metadata
- Download URL: django_post_request_task-0.0.2-py2-none-any.whl
- Upload date:
- Size: 7.4 kB
- Tags: Python 2
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7fb2ea760aa73e97a25564255dd08c7ed16f9e8f1370e601e79c7005555ae94e |
|
MD5 | 51482dbbbc9a8019b29c5f15ab91ed5e |
|
BLAKE2b-256 | 7ef29147e38e59075df90e83c4599ab0070af91ae92b62e8a8598e5ba2fc310d |