Django在没有Cron的情况下设置了一个计划任务

时间:2023-01-07 02:21:16

I know there are many questions asking about this, especially this one: Django - Set Up A Scheduled Job?.

我知道有很多问题要问这个,特别是这个:Django - 设定一份预定的工作?

But what I want to understand is, how does a scheduled task inside Django actually works?

但我想要了解的是,Django中的计划任务实际上是如何工作的?

My simplistic way to think about it is that there's an infinite loop somewhere, something like this (runs every 60 seconds),

我思考它的简单方法是在某处有一个无限循环,就像这样(每60秒运行一次),

import time
interval=60  #60 seconds 
while True:
    some_method()
    time.sleep(interval)

Question: where do you put this infinite loop? Is there some part of the Django app that just runs in the background alongside the rest of the app?

问题:你把这个无限循环放在哪里? Django应用程序的某些部分是否与应用程序的其余部分一起在后台运行?

Thanks!

1 个解决方案

#1


1  

Django doesn't do scheduled tasks. If you want scheduled tasks, you need a daemon that runs all the time and can launch your task at the appropriate time.

Django不执行计划任务。如果您想要计划任务,则需要一个始终运行的守护程序,并且可以在适当的时间启动任务。

Django only runs when a http request is made. If no one makes a http request for a month, django doesn't run for a month. If there are 45 http requests this second, django will run 45 times this second (in the absence of caching).

Django仅在发出http请求时运行。如果没有人发出一个月的http请求,那么django就不会运行一个月。如果此次有45个http请求,则django将在此秒运行45次(在没有缓存的情况下)。

You can write scripts in the django framework (called management commands) that get called from some outside service (like cron). That's as close as you'll get to what you want. If that's the case, then the question/answer you reference is the place to get the how tos.

您可以在django框架中编写脚本(称为管理命令),这些脚本可以从某些外部服务(如cron)调用。这就像你得到你想要的那样接近。如果是这种情况,那么你提到的问题/答案就是获得如何获得的答案。

Probably on a unixy system, cron is the simplest outside service to work with. On recent linux systems, cron has a directory /etc/cron.d into which you can drop your app's cron config file, and it will not interfere with any other cron jobs on the system. No editing of existing files necessary.

可能在unixy系统上,cron是最简单的外部服务。在最近的Linux系统上,cron有一个目录/etc/cron.d,您可以在其中删除应用程序的cron配置文件,它不会干扰系统上的任何其他cron作业。无需编辑现有文件。

#1


1  

Django doesn't do scheduled tasks. If you want scheduled tasks, you need a daemon that runs all the time and can launch your task at the appropriate time.

Django不执行计划任务。如果您想要计划任务,则需要一个始终运行的守护程序,并且可以在适当的时间启动任务。

Django only runs when a http request is made. If no one makes a http request for a month, django doesn't run for a month. If there are 45 http requests this second, django will run 45 times this second (in the absence of caching).

Django仅在发出http请求时运行。如果没有人发出一个月的http请求,那么django就不会运行一个月。如果此次有45个http请求,则django将在此秒运行45次(在没有缓存的情况下)。

You can write scripts in the django framework (called management commands) that get called from some outside service (like cron). That's as close as you'll get to what you want. If that's the case, then the question/answer you reference is the place to get the how tos.

您可以在django框架中编写脚本(称为管理命令),这些脚本可以从某些外部服务(如cron)调用。这就像你得到你想要的那样接近。如果是这种情况,那么你提到的问题/答案就是获得如何获得的答案。

Probably on a unixy system, cron is the simplest outside service to work with. On recent linux systems, cron has a directory /etc/cron.d into which you can drop your app's cron config file, and it will not interfere with any other cron jobs on the system. No editing of existing files necessary.

可能在unixy系统上,cron是最简单的外部服务。在最近的Linux系统上,cron有一个目录/etc/cron.d,您可以在其中删除应用程序的cron配置文件,它不会干扰系统上的任何其他cron作业。无需编辑现有文件。