I’m having trouble figuring out how to do this.
I’m trying to run a python script for a set duration. And every 1/10 of the duration I need it to run something. The problem is this step can take any amount of time to complete. I cannot go over the maximum duration set at the start.
Example:
Duration 20 hours
Interval = 20/10 = 2 hours (This can change if it needs to)
Every two hours it runs function(). function() takes between 0-60 minutes to complete. And then it sleeps. How can I make it so that it continues to run 9 more times, but doesn’t go over the max duration?
Perhaps you can use signals. The idea is you set a signal handler to get called when a SIGALRM is generated. In the signal handler you can call
function. Then you reset the alarm and do it again. This is a basic example just to give you an idea.Once the alarm goes off, you call
function()and then reset the alarm:Or you can use
signal.setitimerto automatically configure interval alarms, however I am not sure how that will work with large timeslices (its used for microsleeps).NOTE: You must call signal.alarm() from the main thread.