(sort of) deterministic timing in Python

Hendrik van Rooyen mail at microcorp.co.za
Fri Aug 17 10:08:09 CEST 2007


 "Paul Rubin" <http://p..id> wrote:


>johnmfisher at comcast.net (John Fisher) writes:
>> mark start time
>> start event
>>         event finishes
>> count time until next interval
>> start second event…
>>
>> rather than this:
>>
>> start event
>>         event finishes
>> sleep for interval
>> start second event
>> ...
>> So how do I accomplish this in Python with a minimum of labour?
>
>Normally I'd use something like:
>
>   from time import time
>
>   t0 = time()
>   start event ... event finishes
>   t1 = time()
>   elapsed = t1 - t0
>   sleep(interval - elapsed)
>   start second event ...
>
>Am I missing something?

Not much - only beware of cases when elapsed is greater than
interval - not sure what time.sleep(negative_number) does.

- Hendrik




More information about the Python-list mailing list