How to combine Celery with asyncio to handle HTTP requests asynchronously?

How to recieve async HTTP requests and process the requests asynchronously using celery? Here is what I have tried, is this the right way to combine async with celery so I can receive async HTTP requests and process them asynchronously

The url is:

urlpatterns = [
    path('api/example/', example, name='example'),
]

The views.py is

async def example(request):
    res = await process_data(request)

    json_data = json.loads(res.content)

    return render(request, "index.html", {"task_id": json_data["task_id"],
                                      "task_status": json_data["task_status"]})



async def process_data(request):

    result = some_CPU_heavy_function.delay("yes")

    return JsonResponse({"task_id": result.id,
                         "task_status": result.status},
                        status=status.HTTP_200_OK)

@shared_task
def some_CPU_heavy_function(some_data):
    return {"reply": "yes"}

And the command from Docker is

command: gunicorn server.asgi:application --bind 0.0.0.0:8000 -w 17 -k uvicorn.workers.UvicornWorker 
Back to Top