Precise timing in Python?

Peter Hansen peter at
Thu Nov 16 15:42:48 CET 2000

Bror Johansson wrote:
> That seems like a thing worth trying. Thank you for the suggestion.
> Python on VxWorks (or QNX) was - and is - my primary approach, but Python on
> Linux might be possible.
> What concerned me most was how Python's virtual machine can adapt to a Real
> Time situation. I now have got some enlightenment on that.

Well, not, that changes *everything*!  You didn't mention you were
prepared to use a *realtime* kernel for your testing software!  Most
posters seem to want to do this kind of thing on Win32 or Linux...

Although I haven't investigated fully, I have looked a fair bit at the
Python interpreter core and I can say that it would not in most
practical situations mess up a realtime environment.  If you have an OS
which can guarantee you the required frequency and phase accuracy of the
10Hz pulse you specified, which VxWorks and QNX definitely can, there is
no reason not to use Python on top of that.  You could, of course, write
code which would deliberately or inadvertently lead to a large amount of
scheduling uncertainty, but for your purposes your code would be quite
consistent and measuring would give you all the guarantee you need.

Timing measurements would be necessary to ensure a sufficient safety
margin (run your Python program repeatedly and make sure that the
variation you see in the latency of its response to your pulse is small
enough to be acceptable), but other than that and maybe disabling the
new garbage collection feature of Python 2.0 (I don't know what the
implementation is, but it could pose a risk to a hard realtime system),
you shouldn't have any problems.

Can anybody who knows describe the nature of the implementation of
garbage collection under 2.0?  Does it run only when the system detects
a cycle of references, or on a thread like Java, or what?

Peter Hansen

More information about the Python-list mailing list