Mailing List Archive

Is a Python event polled or interrupt driven?
In the following code is the event polled by the Python process
running the code or is there something cleverer going on such that
Python sees an interrupt when the input goes high (or low)?



import Adafruit_BBIO.GPIO as GPIO

Pin = "P8_8"
GPIO.setup(Pin, GPIO.IN) # set GPIO25 as input (button)

def my_callback(channel):
if GPIO.input(Pin):
print "Rising edge detected on 25"
else: # if port 25 != 1
print "Falling edge detected on 25"

GPIO.add_event_detect(Pin, GPIO.BOTH, my_callback, 1)


--
Chris Green
·
--
https://mail.python.org/mailman/listinfo/python-list
Re: Is a Python event polled or interrupt driven? [ In reply to ]
On Fri, 13 Oct 2023 at 01:48, Chris Green via Python-list
<python-list@python.org> wrote:
>
> In the following code is the event polled by the Python process
> running the code or is there something cleverer going on such that
> Python sees an interrupt when the input goes high (or low)?
>

This isn't something inherent to Python; it's the specific behaviour
of the library you're using. So I dug through that library a bit, and
ended up here:

https://github.com/adafruit/adafruit-beaglebone-io-python/blob/cf306ed7f9f24111d0949dd60ac232e81241bffe/source/event_gpio.c#L753

which starts a thread:

https://github.com/adafruit/adafruit-beaglebone-io-python/blob/cf306ed7f9f24111d0949dd60ac232e81241bffe/source/event_gpio.c#L662

which appears to make use of epoll for efficient event handling. Edge
detection itself seems to be done here:

https://github.com/adafruit/adafruit-beaglebone-io-python/blob/cf306ed7f9f24111d0949dd60ac232e81241bffe/source/event_gpio.c#L522

I don't know enough about the architecture of the BeagleBone to be
certain, but my reading of it is that most of the work of edge
detection is done by the OS kernel, which then sends the Adafruit
handler a notification via a file descriptor. The secondary thread
waits for those messages (which can be done very efficiently), and in
turn calls the Python callbacks.

In other words, the "something cleverer" is all inside the OS kernel,
and yes, in effect, it's an interrupt.

ChrisA
--
https://mail.python.org/mailman/listinfo/python-list
Re: Is a Python event polled or interrupt driven? [ In reply to ]
Chris Angelico <rosuav@gmail.com> wrote:
> On Fri, 13 Oct 2023 at 01:48, Chris Green via Python-list
> <python-list@python.org> wrote:
> >
> > In the following code is the event polled by the Python process
> > running the code or is there something cleverer going on such that
> > Python sees an interrupt when the input goes high (or low)?
> >
>
> This isn't something inherent to Python; it's the specific behaviour
> of the library you're using. So I dug through that library a bit, and
> ended up here:
>
> https://github.com/adafruit/adafruit-beaglebone-io-python/blob/cf306ed7f9f24111d0949dd60ac232e81241bffe/source/event_gpio.c#L753
>
>
> which starts a thread:
>
> https://github.com/adafruit/adafruit-beaglebone-io-python/blob/cf306ed7f9f24111d0949dd60ac232e81241bffe/source/event_gpio.c#L662
>
>
> which appears to make use of epoll for efficient event handling. Edge
> detection itself seems to be done here:
>
> https://github.com/adafruit/adafruit-beaglebone-io-python/blob/cf306ed7f9f24111d0949dd60ac232e81241bffe/source/event_gpio.c#L522
>
>
> I don't know enough about the architecture of the BeagleBone to be
> certain, but my reading of it is that most of the work of edge
> detection is done by the OS kernel, which then sends the Adafruit
> handler a notification via a file descriptor. The secondary thread
> waits for those messages (which can be done very efficiently), and in
> turn calls the Python callbacks.
>
> In other words, the "something cleverer" is all inside the OS kernel,
> and yes, in effect, it's an interrupt.
>
Wow! Thanks for doing all that research. It sounds as if it may be
more efficient than I thought so may be fast enough. I guess I'll
just have to try some actual code (and hardware) and see how it goes.

Thanks again!

--
Chris Green
·
--
https://mail.python.org/mailman/listinfo/python-list