Scheduling a task on 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()
精彩评论