I’m working on building my own mopidy extension for controlling playback with many buttons and potentiometers with the Raspberry Pi. Because the Raspberry Pi does not have analog input I’m using an analog-to-digital-converter, which I readout with SPI and this Adafruit Module.
My extension needs to continuously (in a
while True loop) read values from the ADC. I set it up to work as follows: if a specific value of one ADC channel is bigger than X I want to call a function, e.g.
self.core.playback.play(). This works in principle. I tried two options:
- running the loop in a simple while loop, which will block all other mopidy actions (Thought maybe Mopidy starts each frontend extension in its own thread),
- running the loop in a new thread will enable mopidy to work on its own and my extension also works on a separate thread - but if i want to stop mopidy with systemctl, it will not properly stop since the separate thread is not stopped and therefore the service will finally ‘fail’ after timeout.
Do I maybe need to run my extension as a
pykka.ThreadingActor ? If so, how? Any ideas of how to solve this issue?
Have you looked at the Pykka docs at https://www.pykka.org/en/latest/ ? I would definitely start there.
You probably want to create your
GPIOManager instance in
on_start() rather than
For ThreadingActor, this method is executed in the actor’s own thread, while init() is executed in the thread that created the actor.
This sounds what I was looking for. I’m quite new to mopidy, so I did not yet check out Pykka - will do now Thanks
Hi again. So what I did is the following:
class Tubipy(pykka.ThreadingActor, core.CoreListener):
from .gpio_input_manager import GPIOManager
self.gpio_manager = GPIOManager(self, self.config['tubipy'])
which does exactly what I wanted. The GPIOManager is executed in a separate thread.
Now, if for some reason, lets say
KeyboardInterrupt, Mopidy stops, the while loop in my GPIOManager still does not stop.
I even set an additional condition to the while loop like
self.active = False
# do something
and in the frontend I tried to stop the while loop by changing
False by implementing the
on_failure() functions, like so:
def on_failure(self, exception_type, exception_value, traceback):
Check out the code on my github repo if you want. Do you know what I am doing wrong?