I have seen seen in many places that the Raspberry Pi does not really work well with realtime control, but I am wondering if reading from input pins can be interrupted by the operating system.

In other words, if I have the following code:

GPIO.add_event_detect(IR_PIN, GPIO.BOTH, callback=cb)

and I run that from a Python program in a while loop - is there any way that the operating system would cause Python to miss an edge detection?

Would the raspberry pi ever pause execution of the executing program for some reason?


Linux as shipped with the Pi is not a real time operating system but version do exist called RTOS or RT Linux and some kernel mods for the Pi have been seen in the wild to give some added ability.

Add onto this, Python is an interpreted language and can suffer from the Global Interpreter locking threads let alone from the OS wanting the CPU for its own tasks.

Python brings its own multi-threading and multi-process libraries to help getting more than one thing done.

You are always limited by the OS, devices attached to the CPU and native programming so ‘real-time’ is not something a multitasking computer is very good at.

In your case, it is always possible to lose data (see Tom Scott on YouTube for why no message can be 100% guaranteed) and this is why check routines and hardware signalling has been part of computing since day one.

Remember some communications subsystems on the Pi are handled by the OS (disk / serial / USB) and they operate at speeds in excess of normal GPIO based data transfers by building in checks and correct hardware design.

If you are looking for robust communication, you need to look at:

  • Interrupt driven capture so the link disrupts the normal program flow not the other way around.
  • Limiting what the machine is doing outside of data communications
  • Have a fast enough processor and memory and disk etc etc to handle all the demands
  • Building control signalling (either hardware or software) into the link
  • Building checks into the data so you know it’s correct or not
  • Look at using buffers (Python Queues being one way) where processing is separate from capture
  • Look at using extra hardware to provide a buffer between the high speed data and the slow speed data.

I think you will be surprised as to the amount of data and speed the Pi can handle BUT it depends on the application and connections.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.