Porting extension to mopidy 3.x

I’m going to prt my simple headless extension to mopidy 3.0:

I had to replace

for playlist in self.core.playlists.playlists.get():


for playlist in self.core.playlists.as_list():

Documentation says, that as_list() returns a list of Ref objects: https://docs.mopidy.com/en/latest/api/core/#mopidy.core.PlaylistsController.as_list

However, logger.debug(self.core.playlists.as_list()) returns

DEBUG    2020-01-10 21:18:46,021 [97165:InputFrontend-9] mopidy_headless.frontend
  <pykka._threading.ThreadingFuture object at 0x7f4b7c32b9a0>

Why it is a ThreadingFuture and not a Python list?

Great to hear that you’re porting another extension to Python 3 :slight_smile:

Short story: You need to change .as_list() to .as_list().get() to get the actual value out of the future object.

Long story: Mopidy uses an actor library called Pykka. self.core is a reference to an ActorProxy for Mopidy’s core “actor”, which runs in another thread than your extension code. All methods on the proxy object returns “futures” for some result that is being concurrently calculated in the other thread. By calling one of the methods and getting a future, the extension’s thread sends a message to the core thread to ask it to do its work and return the result. However, the extension isn’t blocked waiting for the result, it immediately receives a future, which is a handle for the result that will be available one time in the future. Once you call .get() on the future, the extension’s thread blocks everything it is doing until the core API method’s return value is available in the future.

Pykka is a quite small library with good documentation if you want to get more into this.

By the way, if your extension is of general usefulness, i.e. can be useful for others than yourself, please feel free to add it to the new Mopidy extension registry once you’ve updated it to work with Mopidy 3 and Python 3.

Thanks for the explanation. This helped a lot.

1 Like