开发者

Scheduling a task on python

开发者 https://www.devze.com 2022-12-30 08:53 出处:网络
I want to run a program that runs a function every 4 开发者_开发问答hours. What is the least consuming way to do so?Simlest way I can think of (in python since the post is tagged with python):

I want to run a program that runs a function every 4 开发者_开发问答hours. What is the least consuming way to do so?


Simlest way I can think of (in python since the post is tagged with python):

import time

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


You can use sched module

Here are the docs

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


Use the build in timer thread:

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() 
0

精彩评论

暂无评论...
验证码 换一张
取 消