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 a GSource
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 for GSourceFuncs
), which could behave the same as a source created with g_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.