Last active
May 12, 2022 19:17
-
-
Save mhemesath/4693116 to your computer and use it in GitHub Desktop.
MIddleware to allow celery tasks to be batched per request.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
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
Is this for django > 3?