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.
createinto an async function. You should have a loop within thecreatethat runs all your logic asynchronously.