0

I am building a Djano app that processes image in a Celery task. A post inside the view method handles the input and triggers the task. Inside the task, I create a model instance based on the processed image. In the same view, I wanted to return a response of the de-serialization of the model instance. What is the best way to throw this response? Should a frontend expect this on another url? How can I signal my app that the model instance is ready to be served?

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

# tasks.py
@shared_task
def long_running_function(user, data):

    image = Image.objects.create()
    ...
    return image # ?

# views.py
class RequestImage(APIView):

    def post(self, request):
        ...
        image = celery_task_example.delay(
            request.user.id, 
            request.data
        )
        # if image is ready
        serializer = ImageSerializer(image)
        return Response(serializer.data)
Nikko
  • 1,410
  • 1
  • 22
  • 49
  • 1
    does it usefull for you: https://stackoverflow.com/questions/54250610/how-to-wait-for-results-from-a-celery-task-in-django – Sezer BOZKIR Jan 31 '23 at 09:36
  • Thanks! What would be the best approach to send the response instead? What about the frontend waiting for the response? – Nikko Jan 31 '23 at 09:53
  • The best way is controlling the status of task, during this time front-end show like "progress" message, when the task is done, front-end can get the result of task from another endpoint. – Sezer BOZKIR Jan 31 '23 at 10:02
  • It is safe to assume that the frontend can send a unique id for this process and watch the progress of this process using the "id"? – Nikko Jan 31 '23 at 10:06
  • 1
    Read here and decide yourself :) https://stackoverflow.com/questions/9034091/how-to-check-task-status-in-celery – Sezer BOZKIR Jan 31 '23 at 10:11

0 Answers0