Using Django's async views with Celery: Tasks not returning results correctly
Quick question that's been bugging me - I've encountered a strange issue with Hey everyone, I'm running into an issue that's driving me crazy. I'm having trouble integrating Django's async views with Celery tasks. My Django application (version 3.2, with ASGI support) includes an asynchronous view that calls a Celery task to perform a time-consuming operation. The question arises when I try to retrieve the result of the Celery task within the async view. Instead of returning the result, I am getting a `Pending` state, which indicates that the task isn't completed at the time I'm trying to access it. Here's a simplified version of what I'm trying to do: ```python from django.http import JsonResponse from django.views import View from asgiref.sync import sync_to_async from .tasks import long_running_task class AsyncTaskView(View): async def get(self, request): task = long_running_task.delay() # Call Celery task result = await sync_to_async(task.get)() # Try to get the result return JsonResponse({'result': result}) ``` In this code, I'm using `sync_to_async` to wait for the task result, but it seems that the `task.get()` method is blocking the async execution rather than yielding its result as expected. When I test it, I receive the following behavior message: ``` TypeError: 'NoneType' object is not callable ``` I’ve tried using both `get(timeout=10)` and `get()` without arguments, but nothing seems to work. I've also checked the Celery worker logs, and it appears that the task is being executed properly. However, I'm not sure if I'm supposed to handle the result differently in an async Django view. Is there a recommended approach to correctly retrieve results from Celery tasks in an async context? Any insights or examples would be greatly appreciated! Thanks in advance! For context: I'm using Python on Linux. For reference, this is a production microservice. This is for a microservice running on Debian.