Skip to main content
 首页 » 编程设计

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

2025年05月04日34linjiqin

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