I'm writing a simple Python 3 program that uses PyDBus and GLib.
I'm trying to use PyDBus to send a signal whenever an edge event is detected on a Raspberry Pi GPIO pin.
To do that, the function that does this task needs to be called at every iteration of the main loop. I've seen that the timeout_add
function does a similar action; however, timeout_add
runs my function only every interval
milliseconds.
Is there a way to call it at every iteration instead?
Use
g_idle_add()
. It creates aGSource
with no preconditions, and a low priority, so its callback function is executed once per main context iteration, at a lower priority than other pending events.If you need to bump the priority, use
g_idle_add_full()
similarly.As @nemequ says above, though, the architecturally best solution is to write your own
GSource
implementation (see the documentation forGSourceFuncs
), which could behave the same as a source created withg_idle_add()
, or could improve performance from knowing the specifics of the I/O pin you’re querying. It all depends on the API the kernel exposes for that I/O pin, and how that’s pollable from user space.There is documentation on writing a custom
GSource
here.