From e5014cb8c2a463908f27be8a101f3681cb0d14cc Mon Sep 17 00:00:00 2001 From: LukasMicroscopy Date: Tue, 25 Mar 2025 15:31:18 +0100 Subject: [PATCH] added lock to put_image in ndtiff dataset. Heavy threaded writing did lead to double file creation and corrupted files. The lock solves this. --- python/ndstorage/ndtiff_dataset.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/python/ndstorage/ndtiff_dataset.py b/python/ndstorage/ndtiff_dataset.py index fe5a9dc..68bf2d4 100644 --- a/python/ndstorage/ndtiff_dataset.py +++ b/python/ndstorage/ndtiff_dataset.py @@ -44,6 +44,7 @@ def __init__(self, dataset_path=None, file_io: NDTiffFileIO = BUILTIN_FILE_IO, s self.file_io = file_io self._lock = threading.RLock() + self._put_image_lock = threading.Lock() if writable: self.major_version = MAJOR_VERSION self.minor_version = MINOR_VERSION @@ -166,6 +167,8 @@ def read_metadata(self, channel=None, z=None, time=None, position=None, row=None return self._do_read_metadata(axes) def put_image(self, coordinates, image, metadata): + # wait for put_image to finish before calling it again. + self._put_image_lock.acquire() if not self._writable: raise RuntimeError("Cannot write to a read-only dataset") @@ -203,6 +206,7 @@ def put_image(self, coordinates, image, metadata): self._index_file.write(index_data_entry.as_byte_buffer().getvalue()) # remove from pending images del self._write_pending_images[frozenset(coordinates.items())] + self._put_image_lock.release() def finish(self): if self.current_writer is not None: