Compare commits

...

1 Commits

Author SHA1 Message Date
Soulter dc526273f0 fix 2026-01-03 16:25:55 +08:00
+30 -13
View File
@@ -25,6 +25,8 @@ class SharedPreferences:
t = threading.Thread(target=self._sync_loop.run_forever, daemon=True) t = threading.Thread(target=self._sync_loop.run_forever, daemon=True)
t.start() t.start()
self._write_lock = threading.Lock()
async def get_async( async def get_async(
self, self,
scope: str, scope: str,
@@ -167,8 +169,11 @@ class SharedPreferences:
raise ValueError( raise ValueError(
"scope_id and key cannot be None when getting a specific preference.", "scope_id and key cannot be None when getting a specific preference.",
) )
scope = scope or "unknown"
scope_id = scope_id or "unknown"
result = asyncio.run_coroutine_threadsafe( result = asyncio.run_coroutine_threadsafe(
self.get_async(scope or "unknown", scope_id or "unknown", key, default), self.get_async(scope, scope_id, key, default),
self._sync_loop, self._sync_loop,
).result() ).result()
@@ -190,21 +195,33 @@ class SharedPreferences:
def put(self, key, value, scope: str | None = None, scope_id: str | None = None): def put(self, key, value, scope: str | None = None, scope_id: str | None = None):
"""设置偏好设置(已弃用)""" """设置偏好设置(已弃用)"""
asyncio.run_coroutine_threadsafe( scope = scope or "unknown"
self.put_async(scope or "unknown", scope_id or "unknown", key, value), scope_id = scope_id or "unknown"
self._sync_loop,
).result() with self._write_lock:
asyncio.run_coroutine_threadsafe(
self.put_async(scope, scope_id, key, value),
self._sync_loop,
).result()
def remove(self, key, scope: str | None = None, scope_id: str | None = None): def remove(self, key, scope: str | None = None, scope_id: str | None = None):
"""删除偏好设置(已弃用)""" """删除偏好设置(已弃用)"""
asyncio.run_coroutine_threadsafe( scope = scope or "unknown"
self.remove_async(scope or "unknown", scope_id or "unknown", key), scope_id = scope_id or "unknown"
self._sync_loop,
).result() with self._write_lock:
asyncio.run_coroutine_threadsafe(
self.remove_async(scope, scope_id, key),
self._sync_loop,
).result()
def clear(self, scope: str | None = None, scope_id: str | None = None): def clear(self, scope: str | None = None, scope_id: str | None = None):
"""清空偏好设置(已弃用)""" """清空偏好设置(已弃用)"""
asyncio.run_coroutine_threadsafe( scope = scope or "unknown"
self.clear_async(scope or "unknown", scope_id or "unknown"), scope_id = scope_id or "unknown"
self._sync_loop,
).result() with self._write_lock:
asyncio.run_coroutine_threadsafe(
self.clear_async(scope, scope_id),
self._sync_loop,
).result()