I have a threaded class whose loop needs to execute 4 times every second. I know that I can do something like
do_stuff()
time.sleep(0.25)
but the problem is that is doesn’t account for the time it takes to do_stuff(). Effectively this needs to be a real-time thread. Is there a way to accomplish this? Ideally the thread would still be put to sleep when not executing code.
The simple solution
The above will make sure that
workis run with an interval of four times per second, the theory behind this is that it will "queue" a call to itself that will be run 0.25 seconds into the future, without hanging around waiting for that to happen.Because of this it can do it’s work (almost) entirely uninterrupted, and we are extremely close to executing the function exactly 4 times per second.
More about
threading.Timercan be read by following the below link to the python documentation:RECOMMENDED] The more advanced/dynamic solution
Even though the previous function works as expected you could create a helper function to aid in dealing with future timed events.
Something as the below will be sufficient for this example, hopefully the code will speak for itself – it is not as advanced as it might appear.
See this as an inspiration when you might implement your own wrapper to fit your exact needs.