如何向用户提供有关Celery任务执行状态的恒定通知? [英] How to provide user constant notification about Celery's Task execution status?
问题描述
在收到用户的请求
def upload(request):
if "POST" == request.method:
# save the file
task_parse.delay()
# continue
和 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
简而言之,共享任务将从位于名为 main.py
的 uploadapp/
目录中的第三个python文件运行函数 aunit()
>.假设 aunit()
是一个耗时的资源处理过程(例如文件解析).当我将其与celery集成在一起时,它现在完全异步工作,这对我来说是件好事.因此,任务开始-> Celery进程->它完成,然后celery设置状态为Finish.我可以使用 flower
查看.
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
.
但是我想做的是,我想通过django UI通知正在使用我的应用程序的用户,一旦Celery在背面完成处理后,您的任务已完成处理
并将状态设置为SUCCESS.
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.
现在,我知道如果:
1.)我不断请求STATUS,并查看它是否返回SUCCESS.
1.) I constantly request the STATUS and see wheather it returns SUCCESS or not.
我该如何通过芹菜来做到这一点.如何从 views.py
中查询Celery Task的状态,并仅通过celery的python模块异步通知用户?
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 ?
推荐答案
您需要一种实时机制.我建议Firebase.在celery任务结束时,使用boolean = True更新用户ID的Firebase实时DB字段.实施JavaScript函数以侦听Firebase数据库user_id对象的更改->更新UI
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
这篇关于如何向用户提供有关Celery任务执行状态的恒定通知?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!