How to get return from celery task in Django to view?

I am processing a large file using celery task in my Django app. I have an APIView in which when it receives a POST, it will trigger the process. In the celery task, I create a model instance and save. How am I going to receive it back to the APIView so that I can serialize the instance.

For simplicity. This would be my APIVIew.

class RequestFile(APIView):

    def post(self, request):
        ...
        image = long_running_function.delay(
            request.user.id, 
            request.data
        )
        ... # how to get the return from the long_running_function?
        # return model serializer

In my celery task. I am creating a model instance that I would like to de-serialize in my view.

class Image(models.Model):
    name = models.CharField(max_length=50, blank=True)
    ...

@shared_task
def long_running_function(user, data):

    image = Image.objects.create()
    ...

    return image # can I return the model instance here?

Is there a function where I can know when the function is done, and return the model instance or the key/id of the instance created?

Back to Top