Benni’s Run – Threads in the mainloop()

We got a nice sample program from Benjamin, which uses as_threads in event handling!

It uses the simulated RPi.GPIO.add_event_callback (…) of RevPiModIO – The function is known by the RaspberryPi.

In “BennisRun”, a function is started, which successively sets all outputs to True. It uses the sleep () function and therefore takes “much” time. When the input is reset, another function starts, which sets all outputs successively to False.

Normally, all functions triggered by events are completely executed before the mainloop () can continue to run any further events .This is wanted and is realized by executing the functions from the same thread that the mainloop () uses (the process image update runs in another thread and is of course NOT stopped! Unless, when entering the mainloop (), the parameter freeze = True is passed).

For “BennisRun”, this means that only one function must set all outputs to True and only then, the other function can be called, which sets all outputs to False.

UNLESS: We use the parameter “as_thread = True” for reg_event (). This is similar to the behavior of add_event_callback (…).

Then the functions are started as single threads and can run in parallel – if the code is not checked, even the same function multiple times (The function must contain the keyword “thread” as the passing parameter).