Precise timing in Python?

M.-A. Lemburg mal at lemburg.com
Thu Nov 16 11:56:45 EST 2000


Peter Hansen wrote:
> 
> 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.

You should also consider changing the sys.setcheckinterval()
default of 10 to something lower -- this assures that you will
catch signals earlier in case you use them.
 
> 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?

Have a look at Modules/gcmodule.c for all the fine details.
When automatic collection is on, the collector is invoked
after 700 new containers (dicts, tuples, lists) have been 
created.

You can tune the GC mechanism using the gc module and its
API.

-- 
Marc-Andre Lemburg
______________________________________________________________________
Company:                                        http://www.egenix.com/
Consulting:                                    http://www.lemburg.com/
Python Pages:                           http://www.lemburg.com/python/




More information about the Python-list mailing list