Django - 设置预定作业? [英] Django - Set Up A Scheduled Job?

查看:134
本文介绍了Django - 设置预定作业?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用Django进行Web应用程序的工作,如果有办法安排一个工作定期运行,我很好奇。

I've been working on a web app using Django, and I'm curious if there is a way to schedule a job to run periodically.

基本上,我只想运行数据库并自动定期进行一些计算/更新,但是我似乎找不到有关这方面的任何文档

Basically I just want to run through the database and make some calculations/updates on an automatic, regular basis, but I can't seem to find any documentation on doing this.

有没有人知道如何设置?

Does anyone know how to set this up?

澄清:我知道我可以设置一个 cron 这样做,但是如果Django中有一些提供此功能的功能,我很好奇。我希望人们能够自己部署这个应用程序,而无需做很多配置(最好是零)。

To clarify: I know I can set up a cron job to do this, but I'm curious if there is some feature in Django that provides this functionality. I'd like people to be able to deploy this app themselves without having to do much config (preferably zero).

我已经考虑了通过追溯触发这些动作只要检查自上次向站点发送请求以来是否应该运行一个作业,但我希望有一些更清洁的东西。

I've considered triggering these actions "retroactively" by simply checking if a job should have been run since the last time a request was sent to the site, but I'm hoping for something a bit cleaner.

推荐答案

我使用的一个解决方案是这样做:

One solution that I have employed is to do this:

1)创建一个自定义管理命令,例如

python manage.py my_cool_command

2)使用 cron (在Linux上)或(在Windows上)运行我的命令在必要的时间。

2) Use cron (on Linux) or at (on Windows) to run my command at the required times.

这是一个简单的解决方案,不需要安装一个沉重的AMQP堆栈。然而,使用其他答案中提到的芹菜等东西,却有不错的优势。特别是,与Celery一起,不用将应用程序逻辑扩展到crontab文件中,这是非常好的。然而,cron解决方案对于一个中小型应用程序非常有用,而且您不需要大量外部依赖。

This is a simple solution that doesn't require installing a heavy AMQP stack. However there are nice advantages to using something like Celery, mentioned in the other answers. In particular, with Celery it is nice to not have to spread your application logic out into crontab files. However the cron solution works quite nicely for a small to medium sized application and where you don't want a lot of external dependencies.

编辑:

在Windows的更高版本中,对于Windows 8,Server 2012及更高版本,不推荐使用命令中的您可以使用 schtasks.exe 进行相同的使用。

In later version of windows the at command is deprecated for Windows 8, Server 2012 and above. You can use schtasks.exe for same use.

这篇关于Django - 设置预定作业?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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