How to provide user constant notification about Celery's Task execution status?

余生长醉 提交于 2020-02-25 13:24:51

问题


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 ?


回答1:


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



来源:https://stackoverflow.com/questions/50751133/how-to-provide-user-constant-notification-about-celerys-task-execution-status

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!