MCPcopy
hub / github.com/cft0808/edict / atomic_json_read

Function atomic_json_read

scripts/file_lock.py:63–76  ·  view source on GitHub ↗

持锁读取 JSON 文件。

(path: pathlib.Path, default: Any = None)

Source from the content-addressed store, hash-verified

61
62
63def atomic_json_read(path: pathlib.Path, default: Any = None) -> Any:
64 """持锁读取 JSON 文件。"""
65 lock_file = _lock_path(path)
66 lock_file.parent.mkdir(parents=True, exist_ok=True)
67 fd = os.open(str(lock_file), os.O_CREAT | os.O_RDWR)
68 try:
69 _lock_shared(fd)
70 try:
71 return json.loads(path.read_text(encoding='utf-8')) if path.exists() else default
72 except Exception:
73 return default
74 finally:
75 _unlock(fd)
76 os.close(fd)
77
78
79def atomic_json_update(

Callers 12

loadFunction · 0.90
cmd_task_memoFunction · 0.90
cmd_delegateFunction · 0.90
cmd_delegate_resultFunction · 0.90
mainFunction · 0.90
test_write_and_readFunction · 0.90
test_update_creates_fileFunction · 0.90
test_unicode_roundtripFunction · 0.90
_task_source_scoreFunction · 0.90
load_tasksFunction · 0.90

Calls 4

_lock_pathFunction · 0.85
_lock_sharedFunction · 0.85
_unlockFunction · 0.85
closeMethod · 0.80

Tested by 5

test_write_and_readFunction · 0.72
test_update_creates_fileFunction · 0.72
test_unicode_roundtripFunction · 0.72