Event for shutdown


#1

I’m running Mopidy 2.2.1 on a Raspberry PI with Raspian 9.4 (stretch).
I’m building a custom Mopidy frontend that uses an RFID tag read to choose songs.
The RFID reader has an executable file that needs to run in the background in order for RFID tags to be read. So in my Mopidy frontend I startup this executable using a subprocess and read this subprocess’s output in a separate thread.
It all works great.

Except…
To stop the RFID reader I have to close the subprocess and stop the thread and I ideally I want it to be stopped when Mopidy is stopped (using ctrl+c). But it appears that Mopidy doesn’t have an event for shutdown. So when I stop Mopidy now, Mopidy does its teardown, and then hangs, because it’s waiting for my RFID subprocess and thread to end, which they don’t. How can I hook into Mopidy’s shutdown?


#2

Pykka actors have an on_stop method for this. An example of an extension using it can be seen at https://github.com/mopidy/mopidy-spotify/blob/develop/mopidy_spotify/backend.py#L70


#3

Thank you! That did the trick.
I guess I need to deepen my understanding of Pykka.