如何在Django中每天精确地执行一次命令? [英] How to execute a command at exact time once a day in Django?
问题描述
1)用户打开一个页面,其中他需要输入命令
和时间
2)Django应用程序将在每一天的给定时间执行该命令,直到用户关闭调度程序(默认情况下为True)
我遇到的问题是:
1)我应该如何一次执行命令,但每天。为了保存命令和时间,我在我的 models.py
class commands(models.Model):
username = models.ForeignKey(User)
command = models.CharField(max_length = 30)
execution_time = models.DateField()
我有同样的时间,但我没有得到正确的方式在给定时间的每一天执行它
可以使用 pytz
库吗?
为了执行命令,我使用 paramiko
库
PS:我不想使用任何外部库
虽然你可以让你的django应用程序在系统上添加和删除cron作业,但是另外一个django-ish方法要使用芹菜。它是一个可以运行同步和异步任务的任务队列系统。
Celery的一个具体功能是计划任务: http://packages.python.org/celery/userguide/periodic-tasks.html
from datetime import timedelta
CELERYBEAT_SCHEDULE = {
running-every-30-seconds:{
task:tasks.add,
schedule:timedelta(seconds = 30),
args:(16,16)
},
}
他们还有一个更精细的版本的期间任务复制crontab的计划:
from celery.schedules import crontab
CELERYBEAT_SCHEDULE = {
#每星期一早上7:30执行
'每周一至上午': {
'task':'tasks.add',
'schedule':crontab(hour = 7,minute = 30,day_of_week = 1),
'args':(16,16 ),
},
}
芹菜本身是独立的,但是有一个 django-celery 具体的verison
此解决方案的好处是您不需要编辑和维护系统级cron选项卡。这是一个高度集成到django中的解决方案,这个确切的用法。
另外,一个巨大的胜过使用cron是Celery可以缩放与您的系统。如果您正在使用基本系统crontab,则任务将位于承载应用程序的服务器上。但是如果您需要加速您的网站并在5个Web应用程序节点上运行,该怎么办?你需要集中crontab。如果您使用的是芹菜,您可以使用大量选项如何运输和存储任务。它本质上是分布式的,可以同步到所有的应用服务器。它是便携式的。
I am working on a Django web based project in which i need to build a application which work in the following sequence:
1) user open a page in which he need to enter a command
and a time
2) Django application will execute that command at a given time on each day till user off the scheduler (by default it is True)
What i am facing the problem is that :
1) How should i execute the commands on a time but on each day. To save the commands and time i created a following model in my models.py
class commands(models.Model):
username = models.ForeignKey(User)
command = models.CharField(max_length=30)
execution_time = models.DateField()
I have the same time but i am not getting the right way to execute it on each day at the given time
and is it possible to do with pytz
library?
For executing the commands i am using paramiko
library
PS: I don't want to use any external library
While you could have your django app add and remove cron jobs on the system, another more django-ish approach would be to use Celery. It is a task queue system that can run both synch and async tasks.
One specific feature of Celery is scheduled tasks: http://packages.python.org/celery/userguide/periodic-tasks.html
from datetime import timedelta
CELERYBEAT_SCHEDULE = {
"runs-every-30-seconds": {
"task": "tasks.add",
"schedule": timedelta(seconds=30),
"args": (16, 16)
},
}
They also have a more granular version of the period task that replicates the scheduling of a crontab:
from celery.schedules import crontab
CELERYBEAT_SCHEDULE = {
# Executes every Monday morning at 7:30 A.M
'every-monday-morning': {
'task': 'tasks.add',
'schedule': crontab(hour=7, minute=30, day_of_week=1),
'args': (16, 16),
},
}
Celery by itself is stand-alone but there is the django-celery specific verison
The benefit of this solution is that you do not need to edit and maintain a system-level cron tab. This is a solution that is highly integrated into django for this exact use.
Also a huge win over using a cron is that Celery can scale with your system. If you were using a basic system crontab, then the tasks would be located on the server that hosts the application. But what if you needed to ramp up your site and run it on 5 web application nodes? You would need to centralize that crontab. If you are using Celery, you have a large number of options for how to transport and store tasks. It is inherently distributed, and available in sync to all your application servers. It is portable.
这篇关于如何在Django中每天精确地执行一次命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!