设置预定的工作? [英] Set up a scheduled job?

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

问题描述

我一直在使用Django开发网络应用,而我很好奇是否有一种方法可以安排作业定期运行。

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.

有人知道如何设置吗?

要澄清:我知道我可以设置一个 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上)或 at (在Windows上)运行

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

这是一个简单的解决方案,不需要安装沉重的AMQP堆栈。但是,使用其他答案中提到的诸如Celery之类的东西有很好的优势。特别是,使用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及更高版本,不建议使用 at 命令。您可以将 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.

**** UPDATE ****
这是新的链接 django doc的代码,用于编写自定义管理命令

**** UPDATE **** This the new link of django doc for writing the custom management command

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

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