(sort of) deterministic timing in Python

Chris Mellon arkanes at gmail.com
Fri Aug 17 16:13:51 CEST 2007


On 8/17/07, Hendrik van Rooyen <mail at microcorp.co.za> wrote:
>
>  "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.
>

On Windows 2k3, Python 2.5 it sleeps forever (or almost forever? Maybe
a signed/unsigned thing) so yeah, be careful of it.



More information about the Python-list mailing list