在python上安排任务

时间:2010-05-17 19:24:26

标签: python timer scheduled-tasks

我想运行一个每4小时运行一次功能的程序。 最便宜的方式是什么?

3 个答案:

答案 0 :(得分:6)

我能想到的最简单的方法(在python中,因为帖子用python标记):

import time

while True:
  do_task()
  time.sleep(4 * 60 * 60) # 4 hours * 60 minutes * 60 seconds

答案 1 :(得分:4)

您可以使用sched模块

以下是文档

https://docs.python.org/3.4/library/sched.html

答案 2 :(得分:3)

使用内置计时器线程:

from threading import Timer

def function_to_be_scheduled():
   """Your CODE HERE"""

interval = 4 * 60 * 60   #interval (4hours)

Timer(interval, function_to_be_scheduled).start()