How to launch a function at regular time intervals ?

Dave Angel davea at ieee.org
Thu Aug 13 15:17:00 EDT 2009



Grant Edwards wrote:
> <snip>On 2009-08-13, Dave Angel <davea at ieee.org> wrote:
>   
>> Grant Edwards wrote:
>>     
>>> <snip>
>>>     interval = 5.0  # interval in seconds
>>>     next = time.time()
>>>
>>>     while True:
>>>         now = time.time()
>>>         if now < next:      
>>>             time.sleep(now-next)
>>>         print "call_the_function()"
>>>         next += interval
>>>
>>> That will be accurate over the long term with minimal jitter.
>>>   
>>>       
>> <snip>
>> 2) If (occasionally) the function takes longer than the
>>    specified interval time, my approach does catch-up calls so
>>    the average remains the same.
>>     
>
> I'm still confused -- doesn't mine do that as well?
>
>   
Yep.  I missed it again.  Clearly it does the catchup the next time 
around the while loop.

DaveA



More information about the Python-list mailing list