How to increase response time in Django rest framework with Celery?
I am trying to increase the ability of the Django rest framework to handle more requests. I wrapped my Django rest framework with Celery. Any suggestions on how to further increase the handling of the number of RPS?
Here is my code
from celery import current_app
from rest_framework.decorators import api_view
class SomeViewSet(viewsets.ModelViewSet):
queryset = SomeModel.objects.all()
serializer_class = SomeSerializer
@api_view(('POST',))
def some_post_request(request):
serializer = serializer_class(data=request.data)
if serializer.is_valid():
address = serializer.validated_data['somedata']
file_path = "usr/"
with open(file_path, 'wb+') as fp:
for chunk in address:
fp.write(chunk)
result = some_function.delay(file_path, address)
return JsonResponse({"task_id": result.id,
"task_status": result.status},
status=status.HTTP_200_OK)
@api_view(('GET',))
def some_get_request(request, task_id):
task = current_app.AsyncResult(task_id)
context = {'task_status': task.status, 'task_id': task.id}
if task.status == 'PENDING':
return Response({**context}, status=status.HTTP_200_OK)
else:
response_data = task.get()
print(response_data)
return Response({**context, **response_data}, status=status.HTTP_201_CREATED)
I would appreciate complete answers on how to further improve the performance of the celery and Django rest framework by either modifying the above code or suggesting libraries you have used.
For example, I tried using the following and it dropped my RPS from 800 to 450.
from asgiref.sync import sync_to_async
@sync_to_async
@api_view(('POST',))
def some_post_request(request):