Optimize idle wake-ups

This commit is contained in:
Götz 2026-02-28 16:50:44 -05:00
parent fa82f45ef9
commit 5120b938ef
3 changed files with 11 additions and 4 deletions

View file

@ -34,7 +34,7 @@ def main() -> None:
asyncio.run(
listen(config, listener, receivers),
loop_factory=factory.make_loop,
debug=True,
debug=False,
)

View file

@ -3,7 +3,6 @@ from collections.abc import Iterable
from mpd.asyncio import MPDClient
from mpd.base import CommandError
from rich import print as rprint
from yarl import URL
from ..config.model import MpdConfig
@ -26,12 +25,14 @@ class MpdStateListener(Player):
receivers: Iterable[Receiver]
art_cache: MpdArtworkCache
idle_count = 0
last_playback: Playback | None
def __init__(self, cache: URL | None = None) -> None:
self.client = MPDClient()
self.art_cache = (
MpdArtworkCache(self, cache) if cache else MpdArtworkCache(self)
)
self.last_playback = None
async def start(self, conf: MpdConfig) -> None:
self.config = conf
@ -53,6 +54,9 @@ class MpdStateListener(Player):
async for subsystems in self.client.idle(self.WATCHED_SUBSYSTEMS):
# If no subsystems actually changed, we don't need to update the receivers.
if not subsystems:
# MPD/python-mpd2 can occasionally wake idle() without reporting a
# changed subsystem; avoid a hot loop if that happens repeatedly.
await asyncio.sleep(0.1)
continue
self.idle_count += 1
await self.update_receivers()
@ -76,7 +80,9 @@ class MpdStateListener(Player):
state = MpdState(status, current, art)
pb = to_playback(self.config, state)
rprint(pb)
if pb == self.last_playback:
return
self.last_playback = pb
await self.update(pb)
async def update(self, playback: Playback) -> None:

View file

@ -19,7 +19,8 @@ def playback_to_media_item(playback: Playback) -> NSMutableDictionary:
if song := playback.active_song:
nowplaying_info = song_to_media_item(song)
nowplaying_info[MPNowPlayingInfoPropertyPlaybackQueueCount] = playback.queue.length
nowplaying_info[MPNowPlayingInfoPropertyPlaybackQueueIndex] = playback.queue.current
if playback.queue.current is not None:
nowplaying_info[MPNowPlayingInfoPropertyPlaybackQueueIndex] = playback.queue.current
return nowplaying_info