Execute a function periodically, with the period being dynamic

52 views Asked by At

I'm trying to execute a "heartbeat" function every "x" seconds. However, this "x" needs to be variable and changed dynamically when certain conditions are met. How can I do this elegantly in python?

The simplest idea I can think of is to use a while True loop in a thread that sleeps for "x" seconds. And then this variable can be updated within the thread when needed so that the next sleep call is for the new "x" seconds.

I'm not sure if this approach is the most optimal way to achieve the heartbeat and heartbeat "racing" that I want. I would appreciate any helpful insights here, thanks!

0

There are 0 answers