In Python 3, I have written a program that checks if it has been more than 30 seconds since the last time the commands started and executes these commands. At the same time, it checks for other commands that are executed for 10 seconds, and in addition, a condition must be true.
from datetime import datetime
lastExecute1 = datetime(1, 1, 1)
lastExecute2 = datetime(1, 1, 1)
while True:
utcnow = datetime.utcnow()
if (utcnow - lastExecute1).total_seconds() >= 30:
runCommands1()
lastExecute1 = utcnow
if (utcnow - lastExecute2).total_seconds() >= 10 and someCondition:
runCommands2()
lastExecute2 = utcnow
The program works, but the processor load is high (I run on Raspberry Pi) and the processor is heated.
That's why I used a sched module (https://stackoverflow.com/a/474543/6523409). This program also works, but with a lower processor load, and that's good.The problem is that the delay in the previous program was only a few milliseconds, and now it is half a second (the commands are run at 0:0.0, 0:30.5, 1:1.0, 1:31.5 ... instead of 0:0.0, 0:30.0, 1:0.0, 1:30.0 ...). Therefore, within 10 minutes, the delay increases for approximately 10 seconds.