Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Async in django rest framework

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.


Update

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.

like image 644
Mohamed Hamza Avatar asked Dec 24 '18 09:12

Mohamed Hamza


1 Answers

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

like image 140
Saad Aleem Avatar answered Oct 08 '22 02:10

Saad Aleem