2

I integrated my project with celery in this way, inside views.py after receving request from the user

def upload(request):
    if "POST" == request.method:
        # save the file
        task_parse.delay()
        # continue

and in tasks.py

from __future__ import absolute_import
from celery import shared_task
from uploadapp.main import aunit


@shared_task
def task_parse():
    aunit()
    return True

In short, the shared task will run a function aunit() from a third python file located in uploadapp/ directory named main.py. Let's assume that aunit() is a resource heavy process which takes time (like file parsing). As I integrated that with celery, It works totally asynchronously now which is good to me. So, the task start -> Celery process -> It finishes then celery set status to Finish. I can view that using flower .

But what I want to do is that I want to notify the user who is using my app also through django UI that Your Task is done processing as soon as Celery has finished processing at back-side and set status to SUCCESS.

Now, I know this is possible if :

1.) I constantly request the STATUS and see wheather it returns SUCCESS or not.

How do I do that via Celery. How can you query Celery Task status from your views.py and notify user asynchronously with just celery's python module ?

Marcus
  • 105
  • 2
  • 11
  • What about [Django Channels](https://channels.readthedocs.io/en/latest/) – JPG Jun 08 '18 at 03:29
  • `result = AsyncResult(task_id)` `state = result.state` you can return success state in views – Roshan Bagdiya Jun 08 '18 at 08:16
  • @Roshan , That's an alternative but we need to constantly request that `state` from `views.py` or either from UI. How could we do that? – Marcus Jun 08 '18 at 15:57
  • suggestion: Write a polling mechanism on following table `django_celery_results.models.TaskResult `, used to store task results, and you can query this database table like any other Django model. @Marcus – Roshan Bagdiya Jun 08 '18 at 16:05
  • 1
    I found this very helpful https://stackoverflow.com/questions/33935522/updating-client-that-a-celery-task-has-finished/33947913 – mono57 Feb 14 '21 at 18:46

1 Answers1

0

You need a real time mechanism. I would suggest Firebase. Update the Firebase real time DB field of user id with a boolean=True at the end of the celery task. Implement a javascript function to listen to Firebase database user_id object changes -> update the UI

Dmitrii G.
  • 895
  • 1
  • 7
  • 21