From: Marcel van der Veldt Date: Wed, 18 Feb 2026 20:47:32 +0000 (+0100) Subject: Add eager start to create_task helper X-Git-Url: https://git.kitaultman.com/?a=commitdiff_plain;h=961511b0245c54dfc8a1d4a1f87d1a4290cd8ea7;p=music-assistant-server.git Add eager start to create_task helper --- diff --git a/music_assistant/helpers/util.py b/music_assistant/helpers/util.py index 3bd9c3e2..ea5b7423 100644 --- a/music_assistant/helpers/util.py +++ b/music_assistant/helpers/util.py @@ -972,6 +972,7 @@ def guard_single_request[ProviderT: "Provider | CoreController", **P, R]( *args, task_id=task_id, abort_existing=False, + eager_start=True, **kwargs, ) return await task diff --git a/music_assistant/mass.py b/music_assistant/mass.py index 1df492ea..1d1c5a1e 100644 --- a/music_assistant/mass.py +++ b/music_assistant/mass.py @@ -458,11 +458,21 @@ class MusicAssistant: *args: Any, task_id: str | None = None, abort_existing: bool = False, + eager_start: bool = True, **kwargs: Any, ) -> asyncio.Task[_R]: """Create Task on (main) event loop from Coroutine(function). Tasks created by this helper will be properly cancelled on stop. + + :param target: Coroutine function or awaitable to run as a task. + :param args: Arguments to pass to the coroutine function. + :param task_id: Optional ID to track and deduplicate tasks. + :param abort_existing: If True, cancel existing task with same task_id. + :param eager_start: If True (default), start task immediately without waiting + for next event loop iteration. This ensures proper ordering + when creating multiple tasks in sequence. + :param kwargs: Keyword arguments to pass to the coroutine function. """ if task_id and (existing := self._tracked_tasks.get(task_id)) and not existing.done(): # prevent duplicate tasks if task_id is given and already present @@ -474,15 +484,18 @@ class MusicAssistant: if inspect.iscoroutinefunction(target): # coroutine function - task = self.loop.create_task(target(*args, **kwargs)) + coro = target(*args, **kwargs) elif inspect.iscoroutine(target): # coroutine - task = self.loop.create_task(target) + coro = target elif callable(target): raise RuntimeError("Function is not a coroutine or coroutine function") else: raise RuntimeError("Target is missing") + # Use asyncio.Task directly with eager_start for immediate execution + task: asyncio.Task[_R] = asyncio.Task(coro, loop=self.loop, eager_start=eager_start) + if task_id is None: task_id = uuid4().hex