Skip to content

Instantly share code, notes, and snippets.

@mhemesath
Last active May 12, 2022 19:17
Show Gist options
  • Save mhemesath/4693116 to your computer and use it in GitHub Desktop.
Save mhemesath/4693116 to your computer and use it in GitHub Desktop.
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
task.
Example Usage:
===============
>> request.add_async_task(foobar, 1, 2)
>> request.add_async_task(barbaz, 4, 5)
"""
@task()
def execute_tasks(self, tasks):
for task in tasks:
task()
def process_response(self, request, response):
"""
Aggregates all tasks found on the
"""
self.execute_tasks.delay(request._async_tasks)
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
@Angeloem
Copy link

Is this for django > 3?

@n3rio
Copy link

n3rio commented May 12, 2022

Hello, thank you for this, i do have one question, what does add_method means?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment