MCPcopy Create free account
hub / github.com/pytorch/executorch / _atomic_write_binary

Function _atomic_write_binary

backends/mlx/test/test_utils.py:134–155  ·  view source on GitHub ↗

Atomically write binary data to a file. Writes to a temporary file in the same directory, then atomically replaces the target path. This prevents race conditions when multiple parallel workers write to the same ``op_tests/`` tree.

(path: Path, data: bytes)

Source from the content-addressed store, hash-verified

132
133
134def _atomic_write_binary(path: Path, data: bytes) -> None:
135 """
136 Atomically write binary data to a file.
137
138 Writes to a temporary file in the same directory, then atomically replaces
139 the target path. This prevents race conditions when multiple parallel
140 workers write to the same ``op_tests/`` tree.
141 """
142 path.parent.mkdir(parents=True, exist_ok=True)
143 fd, tmp = tempfile.mkstemp(dir=path.parent, suffix=".tmp")
144 closed = False
145 try:
146 os.write(fd, data)
147 os.close(fd)
148 closed = True
149 os.replace(tmp, path)
150 except BaseException:
151 if not closed:
152 os.close(fd)
153 if os.path.exists(tmp):
154 os.unlink(tmp)
155 raise
156
157
158def save_tensors_to_bin(tensors: List[torch.Tensor], path: Union[str, Path]) -> None:

Callers 2

save_tensors_to_binFunction · 0.85
export_model_to_pteFunction · 0.85

Calls 2

writeMethod · 0.80
closeMethod · 0.45

Tested by

no test coverage detected