I've spent some significant time searching for something like this, but I think I'm not using the right search terms. Anyway, I have tasks that I want to do at certain times of the day, and these tasks are executable via a python-api.
Is there a module/method I can use to make sure these tasks run at the correct times, and ensures no tasks are skipped? If I wrote something myself it would look really ugly like:
import sys
import time
taskA = False
taskB = False
taskC = False
while True:
now = time.strftime("%H:%M:%S")
if taskA == False and now >= "10:00:00":
<do TaskA>
taskA = True
if taskB == False and now >= "12:00:00":
<do TaskB>
taskB = True
if taskC == False and now >= "16:20:07":
<do TaskC>
taskC = True
sys.exit(0)
time.sleep(1)
This is something that is currently on cron but I want to replace it with a python script.
IIUC: Here is one simple solution, You can use
threading.Timerfunction from thethreadingpackage which starts a thread that executes a function after a specified interval has passed. You can use this:For Example,
If current time
12:00:00and supposetask_ais scheduled to run at13:00:00,task_bis scheduled to run at13:30:00andtask_cis scheduled to run at14:00:00.. Then after executing the scripttask_awill run after60seconds,task_bwill run after90, andtask_cwill run after120seconds from the current time.Sample Result: