Django / Celery中的定期任务-如何在屏幕上通知用户? [英] Periodic tasks in Django/Celery - How to notify the user on screen?

查看:263
本文介绍了Django / Celery中的定期任务-如何在屏幕上通知用户?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在已经成功设置了Django-celery,以检查我现有的任务,以在任务到期时通过电子邮件提醒用户:

I have now succesfully setup Django-celery to check after my existing tasks to remind the user by email when the task is due:

@periodic_task(run_every=datetime.timedelta(minutes=1))  
def check_for_tasks():      
    tasks = mdls.Task.objects.all()
    now = datetime.datetime.utcnow().replace(tzinfo=utc,second=00, microsecond=00)
    for task in tasks:
        if task.reminder_date_time == now:
            sendmail(...)

到目前为止,到目前为止还不错,但是如果我还想向用户显示弹出窗口作为提醒,该怎么办?

So far so good, however what if I wanted to also display a popup to the user as a reminder?

Twitter引导程序允许创建弹出窗口并从javascript显示弹出窗口:

Twitter bootstrap allows creating popups and displaying them from javascript:

$(this).modal('show');

问题是,芹菜工人守护程序如何在用户的浏览器上运行此javascript?也许我走的是完全错误的方式,这根本不可能。因此,问题仍然存在,是否可以将celery上的cronjob用于在浏览器上实现ui通知?

The problem is though, how can a celery worker daemon run this javascript on the user's browser? Maybe I am going a complete wrong way and this is not possible at all. Therefore the question remains can a cronjob on celery ever be used to achieve a ui notification on the browser?

推荐答案

不要使用 Django消息框架,因为该任务无法实现来访问用户的请求,您也不能将请求对象传递给工作人员,因为它们是不可拾取的。

Well, you can't use the Django messages framework, because the task has no way to access the user's request, and you can't pass request objects to the workers neither, because they're unpickable.

但是您绝对可以使用类似 django-notifications 。您可以在任务中创建通知,并将其附加到相关用户。然后,您可以从视图中检索这些消息,并根据自己的喜好在模板中进行处理。用户将在下一个请求中看到该通知(或者您可以将AJAX轮询用于实时通知或 HTML5 websockets 实时[请参见 django-websocket ])

But you could definitely use something like django-notifications. You could create notifications in your task and attach them to the user in question. Then, you could retrieve those messages from your view and handle them in your templates to your liking. The user would see the notification on their next request (or you could use AJAX polling for real-time-ish notifications or HTML5 websockets for real real-time [see django-websocket]).

这篇关于Django / Celery中的定期任务-如何在屏幕上通知用户?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆