There are several things going on in your code. I suspect that what's really causing the problem is that you need to exit the interrupt handler before another interrupt callback can be triggered...but there is also a confusing mix of callback-based handlers and the GPIO.event_detected method.

I think you can simplify things by performing less manipulation of your interrupt configuration. Just have a state variable that starts at 0, increment it to 1 on the first interrupt, so the next time the interrupt method is called you know it's the second interrupt. No need to try setting multiple handlers like that.

Keeping in mind that I don't actually know what you're trying to do...I imagine something like this:

import RPi.GPIO as GPIO
import time

state = 0

GPIO.setmode(GPIO.BCM)
GPIO.setup(26, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(19, GPIO.IN, pull_up_down=GPIO.PUD_UP)


def interrupt_handler(channel):
    global state

    print("interrupt handler")

    if channel == 19:
        if state == 1:
            state = 0
            print("state reset by event on pin 19")
    elif channel == 26:
        if state == 0:
            state = 1
            print("state set by event on pin 26")


GPIO.add_event_detect(26, GPIO.RISING,
                      callback=interrupt_handler,
                      bouncetime=200)

GPIO.add_event_detect(19, GPIO.RISING,
                      callback=interrupt_handler,
                      bouncetime=200)


while (True):
    time.sleep(0)
Answer from larsks on Stack Overflow
🌐
The Robotics Back-End
roboticsbackend.com › home › raspberry pi gpio interrupts tutorial
Raspberry Pi GPIO Interrupts Tutorial - The Robotics Back-End
December 30, 2021 - We use GPIO.BOTH to get both rising and falling interrupts. In the button_callback() function we check the current button’s state (so, right after the interrupt has been triggered) and power on/off the LED accordingly.
Top answer
1 of 2
2

There are several things going on in your code. I suspect that what's really causing the problem is that you need to exit the interrupt handler before another interrupt callback can be triggered...but there is also a confusing mix of callback-based handlers and the GPIO.event_detected method.

I think you can simplify things by performing less manipulation of your interrupt configuration. Just have a state variable that starts at 0, increment it to 1 on the first interrupt, so the next time the interrupt method is called you know it's the second interrupt. No need to try setting multiple handlers like that.

Keeping in mind that I don't actually know what you're trying to do...I imagine something like this:

import RPi.GPIO as GPIO
import time

state = 0

GPIO.setmode(GPIO.BCM)
GPIO.setup(26, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(19, GPIO.IN, pull_up_down=GPIO.PUD_UP)


def interrupt_handler(channel):
    global state

    print("interrupt handler")

    if channel == 19:
        if state == 1:
            state = 0
            print("state reset by event on pin 19")
    elif channel == 26:
        if state == 0:
            state = 1
            print("state set by event on pin 26")


GPIO.add_event_detect(26, GPIO.RISING,
                      callback=interrupt_handler,
                      bouncetime=200)

GPIO.add_event_detect(19, GPIO.RISING,
                      callback=interrupt_handler,
                      bouncetime=200)


while (True):
    time.sleep(0)
2 of 2
0

That sleep(0) just causes the process to idle there while waiting for switch interrupts. Since nothing in there programmatically ends the process, doing a Ctl-C will stop it.

Discussions

gpio - How do I implement an interrupt service routine on Raspberry Pi? - Raspberry Pi Stack Exchange
There are several libraries like WiringPi, RPi and pigpio, claiming to implement interrupt handling for GPIO signals. But as far as I can estimate, they all do polling on the pins, therefore implem... More on raspberrypi.stackexchange.com
🌐 raspberrypi.stackexchange.com
January 11, 2021
Need Interrupt Help
Working on a Raspberry Python app that has a interrupt that not working as expected. When I start the app (before calling the main function) the push button interrupt works great and as expected. When the main function is called and returns the push button interrupt stops working. More on discuss.python.org
🌐 discuss.python.org
19
0
March 14, 2025
Jetson.GPIO interrupt processing on nano
I have Jetson.GPIO version 0.1.3. trying code similar to button_interupt.py. input on pin 12 only. (no output led) Main loop reads pin 12 every second and prints value. This works fine. GPIO.add_event_detect(12, GPIO.RISING, callback=blink1, bouncetime=10) only works one time, returning to main ok. More on forums.developer.nvidia.com
🌐 forums.developer.nvidia.com
1
0
May 25, 2019
How to handle multiple simultaneous interrupts in Python?
Just gonna toss this out there, and I don't think your gonna like it. Maybe someone else will prove me wrong... working with multiple fast interrupts is best dealt with using a processor that is not hampered by a multitasking operating system. Also "simultaneous" interrupts may better be managed by a single handler that looks a register to determine what to count. Example... if eight interrupts set a bit each in a register and any set bit triggers the handler, you could shift the register along and take action on the set bits. Secondly (and I'm less sure of this) Python may not be the best (fastest) language for your application. C or C++ are much better at bit twiddling and compile to quicker code. Assembly programming would be even faster but not for the faint of heart. I don't really know the parameters around you interrupt handling needs. If they are quite nasty I might realistically I suggest you use one of the many fast microcontrollers to manage the timing and counting and have it pass the data on to the python program. Yours An interrupt lover More on reddit.com
🌐 r/raspberry_pi
17
6
February 27, 2021
🌐
Raspberry Pi Forums
forums.raspberrypi.com › board index › programming › python
how do interrupts work on rpi4 - Raspberry Pi Forums
March 14, 2023 - Linux does handle hardware interrupts, whether they are GPIO level changes or a timer etc. It does not poll for changes. Linux then notifies any software which has registered an interest in that interrupt. That might e.g. be a Python script or a C program etc.
🌐
Medium
medium.com › @rxseger › interrupt-driven-i-o-on-raspberry-pi-3-with-leds-and-pushbuttons-rising-falling-edge-detection-36c14e640fef
Interrupt-driven I/O on Raspberry Pi 3 with LEDs and pushbuttons: rising/falling edge-detection using RPi.GPIO | by R. X. Seger | Medium
August 22, 2016 - The solution, invented in 1957 by the great E. W. Dijkstra: interrupts. ... Sounds complicated, fortunately the RPi.GPIO Python module included in Raspbian supports interrupts nearly as easily as polling.
🌐
pytz
pythonhosted.org › RPIO › rpio_py.html
RPIO, the Python module - GPIO & TCP Interrupts
RPIO.py extends RPi.GPIO in various ways, and uses the BCM GPIO numbering scheme by default. ... RPIO can listen for two kinds of interrupts: GPIO and TCP. GPIO interrupts happen when the state on a specific GPIO input changes.
Find elsewhere
🌐
RasPi.TV
raspi.tv › 2013 › how-to-use-interrupts-with-python-on-the-raspberry-pi-and-rpi-gpio-part-3
How to use interrupts with Python on the Raspberry Pi and RPi.GPIO – part 3
June 22, 2015 - Multiple threaded callback interrupts in Python We’ve been learning about interrupts this week because of the brand new interrupt capabilities of RPi.GPIO. We covered a simple “wait for…
🌐
Quorten Blog 1
quorten.github.io › quorten-blog1 › blog › 2020 › 09 › 12 › rpi-gpio-int-uspace
A more elegant way to get Raspberry Pi GPIO interrupts in user-space | Quorten Blog 1
September 12, 2020 - Well, if it’s a bummer either way, I’m guessing Linuxepoll may be easier if you only have one GPIO pin to monitor, otherwise gpio-keys is better. Linux has been slow to adopt user-space driver functionality, but UIO is one of the newer features that is a step in the right direction. 20200902/https://stackoverflow.com/questions/7986260/linux-interrupt-handling-in-user-space
🌐
MicroPython
docs.micropython.org › en › latest › esp8266 › tutorial › pins.html
6. GPIO Pins — MicroPython latest documentation
We set pin 0 to trigger only on a falling edge of the input (when it goes from high to low), and set pin 2 to trigger on both a rising and falling edge. After entering this code you can apply high and low voltages to pins 0 and 2 to see the interrupt being executed.
🌐
Readthedocs
python-periphery.readthedocs.io › en › latest › gpio.html
GPIO — python-periphery 2.4.1 documentation
from periphery import GPIO # Open GPIO /dev/gpiochip0 line 10 with input direction gpio_in = GPIO("/dev/gpiochip0", 10, "in") # Open GPIO /dev/gpiochip0 line 12 with output direction gpio_out = GPIO("/dev/gpiochip0", 12, "out") value = gpio_in.read() gpio_out.write(not value) gpio_in.close() gpio_out.close()
🌐
Python.org
discuss.python.org › python help
Need Interrupt Help - Python Help - Discussions on Python.org
March 14, 2025 - Working on a Raspberry Python app that has a interrupt that not working as expected. When I start the app (before calling the main function) the push button interrupt works great and as expected. When the main function …
🌐
NVIDIA Developer Forums
forums.developer.nvidia.com › robotics & edge computing › jetson systems › jetson nano
Jetson.GPIO interrupt processing on nano - Jetson Nano - NVIDIA Developer Forums
I have Jetson.GPIO version 0.1.3. trying code similar to button_interupt.py. input on pin 12 only. (no output led) Main loop reads pin 12 every second and prints value. This works fine. GPIO.add_event_detect(12, GPIO.R…
Published   May 25, 2019
🌐
Reddit
reddit.com › r/raspberry_pi › how to handle multiple simultaneous interrupts in python?
r/raspberry_pi on Reddit: How to handle multiple simultaneous interrupts in Python?
February 27, 2021 -

Hello, so this question is about both the Raspberry Pi and Python. I'm building a monitoring circuit that needs to monitor multiple sensors at once, and report the data back over MQTT. The main problem I'm having is that the RPi.GPIO interrupts only support one thread at a time to run the callback function triggered by interrupts, and the callback functions can only be executed sequentially, not simultaneously. This won't work because I have multiple sensors that I need to count the rising edges for and calculate the amount of times they happen over a given time period.

I've tried using multiprocessing and multithreading a couple of different ways, but it's not quite working how I need it to. Here's the basic idea for the code:

# Global Variables
THREAD_EXECUTE = True # Used to tell the threads when to stop running
SENSOR_1_TRIGGERS = 0 # Used to track number of rising edge triggers
SENSOR_2_TRIGGERS = 0 # Used to track number of rising edge triggers

def callback_1(input_pin):
    global THREAD_EXECUTE
    global SENSOR_1_TRIGGERS

    while THREAD_EXECUTE:
        GPIO.wait_for_edge(input_pin, GPIO.RISING, bouncetime=100)
        SENSOR_1_TRIGGERS += 1

def callback_2(input_pin):
    global THREAD_EXECUTE
    global SENSOR_2_TRIGGERS

    while THREAD_EXECUTE:
        GPIO.wait_for_edge(input_pin, GPIO.RISING, bouncetime=100)
        SENSOR_2_TRIGGERS += 1

def main():
    global THREAD_EXECUTE
    try:
        sensor_1_thread = Thread(target=callback_1, args=(pin_1,))
        sensor_1_thread.start()

        sensor_2_thread = Thread(target=callback_2, args=(pin_2,))
        sensor_2_thread.start()

    except KeyboardInterrupt:
        THREAD_EXECUTE = False
    
    finally:
        sensor_1_thread.join()
        sensor_2_thread.join()
        GPIO.cleanup()
        exit()

Basically I just need a way to monitor multiple interrupts simultaneously, and handle the calculations in their own threads. The above code kind of works, but it's very slow and still doesn't really handle each sensor input independently. What's the best way to do this on the RPi with Python?

Edit for clarity: This project needs to monitor wind speed, wind direction, rainfall, temp, humidity, barometric pressure, battery voltage, and solar panel voltage. Also it needs to control a relay that powers an exhaust fan. The wind speed sensor and rainfall sensor are the two main sensors that really need fast interrupt responses. Everything else can be run on a polling schedule.

🌐
Raspberrypi-aa
raspberrypi-aa.github.io › session2 › input.html
Session 2- Polled and Interrupt Driven Input
Interrupt driven input means taking an action when an input changes in a desired way. Polled input is the simplest method of input on the Raspberry Pi. For a desired pin, the state of the input pin is checked for the input value. If the input value changes, the program can change its behavior.
🌐
GitHub
github.com › NVIDIA › jetson-gpio › blob › master › samples › button_interrupt.py
jetson-gpio/samples/button_interrupt.py at master · NVIDIA/jetson-gpio
A Python library that enables the use of Jetson's GPIOs - jetson-gpio/samples/button_interrupt.py at master · NVIDIA/jetson-gpio
Author   NVIDIA
🌐
YouTube
youtube.com › codelearn
Use Python Asyncio to wait for GPIO interrupt - YouTube
Download this code from https://codegive.com Title: Using Python Asyncio to Wait for GPIO Interrupts - A Step-by-Step TutorialIntroduction:Python's asyncio l...
Published   November 25, 2023
Views   25
🌐
GitHub
github.com › vsergeev › python-periphery › issues › 12
gpio interrupts? · Issue #12 · vsergeev/python-periphery
October 31, 2017 - is this library support gpio interrupts handling ??
Author   jhonoryza
🌐
Electrocredible
electrocredible.com › home › embedded systems › raspberry pi pico › raspberry pi pico interrupts tutorial- examples in micropython
Raspberry Pi Pico Interrupts Tutorial- Examples in MicroPython
April 10, 2025 - Here we discuss how to trigger external interrupts on Raspberry Pi Pico and interface push buttons using polling and interrupts. MicroPython will be used in this tutorial.
🌐
Adafruit
blog.adafruit.com › 2013 › 03 › 22 › how-to-use-interrupts-with-python-on-the-raspberry-pi-and-rpi-gpio
How to use interrupts with Python on the Raspberry Pi and RPi.GPIO
March 22, 2013 - How to use interrupts with Python on the Raspberry Pi and RPi.GPIO This is the first in a series of articles which aim to show you how to use this new interrupt facility in Python. Interrupts are a…