ml.utils.atomic

Defines some utility functions for atomic file operations.

ml.utils.atomic.fsync_directory(path: Path) None[source]

Performs an fsync on a directory.

Parameters:

path – The path to fsync.

ml.utils.atomic.atomic_save(save_func: Callable[[Path], None], save_path: str | Path, durable: bool = False) None[source]

Performs an atomic save using a temporary file.

Parameters:
  • save_func – The function to call, that saves the file

  • save_path – Where to save the file

  • durable – If set, make the write durable

ml.utils.atomic.tempfile(suffix: str = '', dir: str | Path | None = None) Iterator[str][source]

Creates a temporary file and yields its path.

Parameters:
  • suffix – The suffix to use for the temporary file

  • dir – The directory to create the temporary file in

Yields:

The path to the temporary file

Raises:

OSError – If the temporary file could not be created

ml.utils.atomic.open_atomic(filepath: str | Path, mode: BinaryModes, *, encoding: str = 'utf-8', fsync: bool = False) ContextManager[BinaryIO][source]
ml.utils.atomic.open_atomic(filepath: str | Path, mode: TextModes, *, encoding: str = 'utf-8', fsync: bool = False) ContextManager[TextIO]

Opens a file for writing, atomically.

Parameters:
  • filepath – The path to the file to open

  • mode – The mode to open the file in

  • encoding – The encoding to use

  • fsync – If set, make the write durable

Yields:

A context manager that yields the opened file