From: Marcel van der Veldt Date: Tue, 14 Oct 2025 18:54:03 +0000 (+0200) Subject: Dump config to/from json in executor X-Git-Url: https://git.kitaultman.com/?a=commitdiff_plain;h=f9cbb4398c4514fddcb0ee49ac139a86564618e9;p=music-assistant-server.git Dump config to/from json in executor --- diff --git a/music_assistant/controllers/cache.py b/music_assistant/controllers/cache.py index 69931d81..9171d80c 100644 --- a/music_assistant/controllers/cache.py +++ b/music_assistant/controllers/cache.py @@ -19,7 +19,7 @@ from music_assistant_models.enums import ConfigEntryType from music_assistant.constants import DB_TABLE_CACHE, DB_TABLE_SETTINGS, MASS_LOGGER_NAME from music_assistant.helpers.api import parse_value from music_assistant.helpers.database import DatabaseConnection -from music_assistant.helpers.json import json_dumps, json_loads +from music_assistant.helpers.json import async_json_loads, json_dumps from music_assistant.models.core_controller import CoreController if TYPE_CHECKING: @@ -123,7 +123,7 @@ class CacheController(CoreController): ) ) and (not checksum or (db_row["checksum"] == checksum and db_row["expires"] >= cur_time)): try: - data = await asyncio.to_thread(json_loads, db_row["data"]) + data = await async_json_loads(db_row["data"]) except Exception as exc: LOGGER.error( "Error parsing cache data for %s: %s", diff --git a/music_assistant/controllers/config.py b/music_assistant/controllers/config.py index 58ee0db4..88ee22ea 100644 --- a/music_assistant/controllers/config.py +++ b/music_assistant/controllers/config.py @@ -66,7 +66,7 @@ from music_assistant.constants import ( ENCRYPT_SUFFIX, ) from music_assistant.helpers.api import api_command -from music_assistant.helpers.json import JSON_DECODE_EXCEPTIONS, json_dumps, json_loads +from music_assistant.helpers.json import JSON_DECODE_EXCEPTIONS, async_json_dumps, async_json_loads from music_assistant.helpers.util import load_provider_module if TYPE_CHECKING: @@ -915,7 +915,7 @@ class ConfigController: for filename in (self.filename, f"{self.filename}.backup"): try: async with aiofiles.open(filename, encoding="utf-8") as _file: - self._data = json_loads(await _file.read()) + self._data = await async_json_loads(await _file.read()) LOGGER.debug("Loaded persistent settings from %s", filename) await self._migrate() return @@ -1043,7 +1043,7 @@ class ConfigController: await rename(self.filename, filename_backup) async with aiofiles.open(self.filename, "w", encoding="utf-8") as _file: - await _file.write(json_dumps(self._data, indent=True)) + await _file.write(await async_json_dumps(self._data, indent=True)) LOGGER.debug("Saved data to persistent storage") @api_command("config/providers/reload") diff --git a/music_assistant/helpers/json.py b/music_assistant/helpers/json.py index 96e93470..f1aa4e96 100644 --- a/music_assistant/helpers/json.py +++ b/music_assistant/helpers/json.py @@ -58,8 +58,19 @@ def json_dumps(data: Any, indent: bool = False) -> str: ).decode("utf-8") +async def async_json_dumps(data: Any, indent: bool = False) -> str: + """Dump json string async.""" + return await asyncio.to_thread(json_dumps, data, indent) + + json_loads = orjson.loads + +async def async_json_loads(data: str) -> Any: + """Load json string async.""" + return await asyncio.to_thread(json_loads, data) + + TargetT = TypeVar("TargetT", bound=DataClassORJSONMixin)