如何在Django中使用线程 [英] How to use thread in Django
问题描述
我想在一定时间内查看用户的订阅日期。并发送邮件给订阅正在完成的用户(例如提醒两天)。
我认为最好的方法是使用线程和计时器来检查日期。但是我不知道如何调用这个函数。我不想制作一个单独的程序或shell。我想结合这个程序到我的django代码。我试图在我的settings.py文件中调用这个函数。但似乎这不是一个好主意。它调用该函数并在每次导入设置时创建线程。
I want to check users' subscribed dates for certain period. And send mail to users whose subscription is finishing (ex. reminds two days). I think the best way is using thread and timer to check dates. But I have no idea how to call this function. I don't want to make a separate program or shell. I want to combine this procedure to my django code. I tried to call this function in my settings.py file. But it seems it is not a good idea. It calls the function and creates thread every time I imported settings.
推荐答案
使用 Django-cron 更容易和简单
编辑:从django_cron导入cronScheduler,添加一个提示
Added a tip
from django_cron import cronScheduler, Job
class sendMail(Job):
# period run every 300 seconds (5 minutes)
run_every = 300
def job(self):
# This will be executed every 5 minutes
datatuple = check_subscription_finishing()
send_mass_mail(datatuple)
//and just register it
cronScheduler.register(sendMail)
这篇关于如何在Django中使用线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!