Waht do you think about my repeated_timer class

Barry barry at barrys-emacs.org
Thu Feb 3 16:42:32 EST 2022



> On 3 Feb 2022, at 04:45, Cecil Westerhof via Python-list <python-list at python.org> wrote:
> 
> Have to be careful that timing keeps correct when target takes a 'lot'
> of time.
> Something to ponder about, but can wait.

You have noticed that your class does call the function at the repeat interval but
rather at the repeat interval plus processing time.

The way to fix this is to subtract the last processing elapsed time for the next interval.
Sort of a software phase locked loop.

Just before you call the run function record the time.time() as start_time.
Then you can calculate next_interval = max( .001, interval - time.time() - start_time)
I use 1ms as the min interval.

Barry




More information about the Python-list mailing list