(sort of) deterministic timing in Python

Matimus mccredie at gmail.com
Mon Aug 13 19:28:45 EDT 2007


> Do you see the difference? I get a true fixed interval from the first,
> including the time to accomplish the event task(s). In the second case,
> the sleep just gets tacked on at the end of the events, not very
> deterministic (timing-wise).

Check out the sched (scheduler) module http://docs.python.org/lib/module-sched.html.

Here is an example that shows how to implement a loop (modified from
the documentation):
(warning, this will run forever)

[code]
import sched, time
s=sched.scheduler(time.time, time.sleep)

def print_time():
    s.enter(1,1, print_time, ())
    print "From print_time", time.time()

def print_some_times():
    print time.time()
    s.enter(5, 1, print_time, ())
    s.run()

if __name__ == "__main__":
    print_some_times()
[/code]




More information about the Python-list mailing list