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: