0

While looking into a solution for the post GPIO event detect not giving output when button pressed I found a couple of other posts on the subject of call backs in python for the Raspberry Pi.

These posts made me wonder what is the underlying implementation used in python for handling a call back and what are the limitations of this implementation?

A post on the Raspberry Pi forum (see https://www.raspberrypi.org/forums/viewtopic.php?t=128510 ) indicated that there is a single thread, label it the call back thread, which is used for processing all call backs so should there be multiple events triggering a call back, there is a kind of queue and the call back thread removes call back requests from the queue and processes each within the call back thread and not the main thread or any other thread. This implies that should a call back perform some kind of a lengthy calculation then any other call backs waiting to be processed will be delayed.

However there is no link to documentation in the post and the post is from 2015 so perhaps that was python2.

One of the above StackOverflow posts, Raspberry Pi RPi.GPIO error with threated callback , describes a problem with using os.system() in a call back function and when the os.system() function is called in the main script rather than the call back function, it works fine.

So what is the implementation of the call back functionality in python2 and python3? What are some of the limitations for a call back function e.g.:

  • minimize calculations and time taken as much as possible
  • minimize functions called and nesting depth
  • minimize local variables used and the size of those variables
Richard Chambers
  • 16,643
  • 4
  • 81
  • 106
  • Your post implies that there is a) a special Python for Raspberry Pi and b) a special callback implementation in Python. Both don't exist. How callbacks are implemented is a matter of the library you are using. – Klaus D. Feb 12 '21 at 04:09
  • @KlausD. I limited my post to the Raspberry Pi because that is the area I was investigating. This article, https://en.wikipedia.org/wiki/Python_(programming_language)#Implementations , indicates there are a variety of python dialects and implementations. It makes sense that there would be some kind of a standard about how to write a function whose interface uses a callback as well as some kind of machinery for invoking a callback. – Richard Chambers Feb 12 '21 at 04:46

0 Answers0