gpt4 book ai didi

python - 使django应用程序在特定时间通知人们

转载 作者:太空宇宙 更新时间:2023-11-04 08:06:30 24 4
gpt4 key购买 nike

Short description

如何让我的应用在事件创建一定时间后向列表中的人员发送通知?

Long description

我的网站允许 Bob 将他想在线销售的东西放在网上。好几个人看到都想买。如果 6 小时后没有人感兴趣,则应通过电子邮件通知卖家。

Solution I found that I think dirty

每分钟运行一次 crontab 以检查人们不感兴趣的全部文章。缺点:不太准确+扫描整个数据库+在我的应用程序之外......

有没有更好的解决方案?

最佳答案

您要找的是https://pypi.python.org/pypi/django-cron/0.3.6

本质上,您在 INSTALLED_APPS 中使用 django_cron 作为

INSTALLED_APPS = (
'django_cron',)

通过在您的项目根目录中创建一个 cron.py 来设置一个 cron 作业

class SixHourEmailUpdate(CronJobBase):
code = 'six_hour_reminder_cron'
schedule = Schedule( run_every_mins = 360 ) # 6 hours

def do(self):
# Your email sending

此外,您还必须设置一个 cron 作业,其中包含每隔一段时间运行一次的内容。

*/5 * * * * bash /home/tester/my_cron/cron.sh

cron.sh 反过来调用

python /home/tester/app/my_app/manage.py runcrons

更多文档在这里 http://django-cron.readthedocs.org/en/latest/sample_cron_configurations.html#run-at-times-feature

关于python - 使django应用程序在特定时间通知人们,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30393307/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com