Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
MIddleware to allow celery tasks to be batched per request.
from celery import task
class AsyncTaskMiddleware(object):
Dynamically adds an async task queue to the request to be executed when the
response is processed.
All tasks added to the request are executed using a single asynchronous celery task.
Once the task is executed, it will execute each individual task in its own celery
Example Usage:
>> request.add_async_task(foobar, 1, 2)
>> request.add_async_task(barbaz, 4, 5)
def execute_tasks(self, tasks):
for task in tasks:
def process_response(self, request, response):
Aggregates all tasks found on the
return response
def process_request(self, request):
Dynamically adds the async task array and add_async_task method. to the request.
request._async_tasks = []
def add_async_task(self, task, *args, **kwargs):
request._async_tasks.append(partial(task, *args, **kwargs))
add_method(request, add_async_task, 'add_async_task')
return None
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.