waveform80 / rpi-lgpio

A compatibility shim for lgpio emulating the RPi.GPIO API
https://rpi-lgpio.readthedocs.io/
Other
9 stars 5 forks source link

difference in lgpio vs GPIO: not working in MP setups #14

Open Brun059 opened 2 months ago

Brun059 commented 2 months ago

lgpio is greate as it made event_detect to work (again), thanks and thanks https://raspberrypi.stackexchange.com/questions/147332/rpi-gpio-runtimeerror-failed-to-add-edge-detection for pointing me to it.

Yet, lgpio behaves differently in multiprocessing setup. The "old" GPIO allowed to use [and change] GPIO settings across processes in a multiprocessing python while lgpio confines them to the respective running process, actualy the main process only.

In other words, in GPIO I could setup all GPIOs in the main process, briefly flip one of them from output to input and back in a subprocess and read its value in both, the main and the subprocess. With lgpio I can only access a GPIO within and from the respective subprocess. I cannot read its value from both, the main and subprocess. Attempts to initialize them globally and change one from out to in within the subprocess results in lgpio.errer: 'GPIO busy'

Furthermore event_detect is not working in multiprocessing subprocesses, only in the main thread. This means I cannot create and run a process that's continually monitoring some events and feeding the result in an mp.value to the main process/logic.

(reason for doing this wired thing: I use the same PIN as output and briefly as input as well. This to control an external relay in a specific way manually when RPI is powered off and trough RPI if it's on, some diodes ensure that I'm not producing an electrical shortcut)

waveform80 commented 1 month ago

I won't be able to fix this given this is a fundamental difference in how gpio works with the gpiochip devices: pins are reserved by a process and a separate process cannot then access that pin until the first process releases it or dies.

That said, multiprocessing is a module that should be explicitly mentioned in the corresponding documentation section.

Treating this as a documentation enhancement, because I'm afraid the functionality is a case of "won't/can't fix"