Real-time operating via Python

Real-time operating via Python

When people talk about real-time computing, what they mean is that the latency from an interrupt (most commonly set off by a timer) to application code handling that interrupt being run, is both small and predictable. This then means that a control process can be run repeatedly at very precise time intervals or, as in your case, external events can be timed very precisely. The variation in latency is usually called jitter – 1ms maximum jitter means that an interrupt arriving repeatedly will have a response latency that varies by at most 1ms.

Small and predictable are both relative terms and when people talk about real-time performance they might mean 1μs maximum jitter (people building inverters for power transmission care about this sort of performance, for instance) or they might mean a couple of milliseconds maximum jitter. It all depends on the requirements of the application.

At any rate, Python is not likely to be the right tool for this job, for a few reasons:

  • Python runs mostly on desktop operating systems. Desktop operating systems impose a lower limit on the maximum jitter; in the case of Windows, it is several seconds. Multiple-second events dont happen very often, every day or two, and youd be unlucky to have one coincide with the thing youre trying to measure, but sooner or later it will happen; jitter in the several-hundred-milliseconds region happens more often, perhaps every hour, and jitter in the tens-of-milliseconds region is fairly frequent. The numbers for desktop Linux are probably similar, though you can apply different compile-time options and patch sets to the Linux kernel to improve the situation – Google PREEMPT_RT_FULL.
  • Pythons stop-the-world garbage collector makes latency non-deterministic. When Python decides it needs to run the garbage collector, your program gets stopped until it finishes. You may be able to avoid this through careful memory management and carefully setting the garbage collector parameters, but depending on what libraries you are using, you may not, too.
  • Other features of Pythons memory management make deterministic latency difficult. Most real-time systems avoid heap allocation (ie Cs malloc or C++s new) because the amount of time they take is not predictable. Python neatly hides this from you, making it very difficult to control latency. Again, using lots of those nice off-the-shelf libraries only makes the situation worse.
  • In the same vein, it is essential that real-time processes have all their memory kept in physical RAM and not paged out to swap. There is no good way of controlling this in Python, especially running on Windows (on Linux you might be able to fit a call to mlockall in somewhere, but any new allocation will upset things).

I have a more basic question though. You dont say whether your button is a physical button or one on the screen. If its one on the screen, the operating system will impose an unpredictable amount of latency between the physical mouse button press and the event arriving at your Python application. How will you account for this? Without a more accurate way of measuring it, how will you even know whether it is there?

Python is not, by purists standards, a real-time language- it has too many libraries and functions to be bare-bones fast. If youre already going through an OS though, as opposed to an embedded system, youve already lost a lot of true real time capability. (When I hear real time I think the time it takes VHDL code to flow through the wires of an FPGA. Other people use it to mean I hit a button and it does something that is, from my slow human perspective, instantaneous. Ill assume youre using the latter interpretation of real time.)

By stimulus display and button press detection I assume you mean you have something (for example) like a trial where you show a person an image and have them click a button to identify the image or confirm that theyve seen it- perhaps to test reaction speed. Unless youre worried about accuracy down to the millisecond (which should be negligible compared to the time for a human reaction) you would be able to do a test like this using python. To work on the GUI, look into Tkinter: http://www.pythonware.com/library/tkinter/introduction/. To work on the timing between stimulus and a button press, look at the time docs: http://docs.python.org/library/time.html

Good luck!

Real-time operating via Python

Because you are trying to get a scientific measurement on a time delay in millisecond precision, I cannot recommend any process that is subject to time slicing on a general purpose computer. Whether implemented in C, or Java, or Python, if it runs in a time-shared mode, then how can the result be verifiable? You could be challenged to prove that the CPU never interrupted the process during a measurement, thereby distorting the results.

It sounds like you may need to construct a dedicated device for this purpose, with a clock circuit that ticks at a known rate and can measure the discrete number of ticks that occur between stimulus and response. That device can then be controlled by software that has no such timing constraints. Maybe you should post this question to the Electrical Engineering exchange.

Without a dedicated device, you will have to develop truly real-time software that, it terms of modern operating systems, runs within the kernel and is not subject to task switching. This is not easy to do, and it takes a lot of effort to get it right. More time, I would guess, than you would spend building a dedicated software-controllable device for your purpose.

Leave a Reply

Your email address will not be published. Required fields are marked *