I have a Python script using callback functions to react to GPIO events, as described here: https://sourceforge.net/p/raspberry-gpio-python/wiki/Inputs/

The description states that Python runs the callback functions in separate threads.

The question I have is what kind of Python threads these are (e.g. the same as that defined by the threading module?) and if the GPIO python module (or another one) provides means for communication between these threads to, e.g., check if the previous thread has finished or how many threads are running.


Your question is about the RPi.GPIO Python module.

The source code may be downloaded.

The module is implemented using the C programming language.

The functionality you are asking about is implemented in the event_gpio.c source file.

As far as I can tell a single thread (C pthread) is created to handle all callbacks.

As far as I am aware the other Python GPIO modules also use a single thread to handle callbacks. To be sure you would have to examine the source code for each Python GPIO module (a partial list will be RPi.GPIO, gpiozero, wiringPi, RPIO.GPIO and (my) pigpio).

  • Actually on the page OP links to it says the callback functions are run sequentially, not concurrently. This is because there is only one thread used for callbacks, in which every callback is run, in the order in which they have been defined. – Dirk Sep 13 '18 at 8:31
  • @Dirk That seems definitive. I'm not sure where but I have certainly read statements incorrectly suggesting there are multiple threads. – joan Sep 13 '18 at 8:36
  • Thanks, that is valuable information for me. Does this mean that there is a kind of queue for the callbacks (or perhaps that happens low-level)? Do I have a chance to obtain information about this thread (how many callbacks are due etc.) without modifying RPi.GPIO ? – highsciguy Sep 13 '18 at 11:04
  • 1
    @highsciguy - Yes, as it says on the page they are queued / run in order they come in. You should exit the callback as soon as possible to let waiting callbacks time to run. I don't think you can get any extra info without modification of the source code – Dirk Sep 13 '18 at 11:19

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.