How to implement asynchronous tasks in Django rest framework? After python3.7 async.io became part of the python language and coroutines are embedded in the language .
But I can’t make use out of it i had to use celery and a redis server for such async behavior.
class ReportViewSet(viewsets.ModelViewSet):
queryset = Report.objects.all()
serializer_class = ReportSerializer
filter_class = ReportFilter
def create(self, request):
serializer = ReportSerializer(data=request.data)
if serializer.is_valid(raise_exception=True):
report_obj = serializer.save()
#Start multiple tools asynchronously but we need to return the next statement without waiting for those tools to finish
return Response(serializer.data, status=status.HTTP_201_CREATED)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
I tried to modify the create an async function
that runs the tools, but i get the error that you can't run this function in an executor that's not a co-routine. When i tried to make the create
function async, it returns a co-routine instead of an HTTP response
. So the django-rest-framework
itself needs to modify its internals to be of co-routine types. Any suggesstions or thoughts on how to do what i mentioned in a good way without using any MQ or caching techniques.
You shouldn't be turning create
into an async function. You should have a loop within the create
that runs all your logic asynchronously but even that won't free up the worker process to serve other requests while your async tasks run. The best you can do here is offload the tasks to celery and then poll through another API to check for their status or result.
Django's a synchronous framework and it won't support what you're trying to do here. If you want to have long-polling with overlapping requests then I'd suggest looking into tornado
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With