Today all programs in python for Raspberry boil down to:

while (True):

But is there anything like listening to GPIO ports asynchronously? Because in the previous form I consider a lot of 'structural programming' and if something happens down there it may not be read during the While.

Use something like the AsyncIO library?

An example of a switch button

import RPi.GPIO as GPIO
GPIO.setmode (GPIO.BCM)
GPIO.setup (17, GPIO.IN)

while True:
   if (GPIO.input (17)):
  // if he presses the button again he will still be in this process.

Is there something like the Delegate in C #?

  • 1
    I have taken the liberty to remove the part that was posted twice in your question and edit a few capitalization errors with Python keywords.
    – Ghanima
    Apr 18, 2017 at 19:42

1 Answer 1


You need to use callbacks by whatever name they have in your Python GPIO library.

My pigpio library uses callback.

The equivalent in RPi.GPIO is event detect.

For a pigpio callback usage example see http://abyz.me.uk/rpi/pigpio/examples.html#Python_monitor_py which prints the GPIO, new level, and microseconds since the last change whenever a GPIO changes state.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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