diff --git a/cadet/__init__.py b/cadet/__init__.py index 439b27f..93297b3 100644 --- a/cadet/__init__.py +++ b/cadet/__init__.py @@ -1,6 +1,6 @@ name = 'CADET-python' -__version__ = 0.7 +__version__ = 0.8 from .cadet import H5 from .cadet import Cadet diff --git a/cadet/cadet.py b/cadet/cadet.py index cbee6d3..d8075f3 100644 --- a/cadet/cadet.py +++ b/cadet/cadet.py @@ -10,6 +10,9 @@ import copy import json +import filelock +import contextlib + from pathlib import Path class H5(): @@ -27,21 +30,34 @@ def __init__(self, *data): for i in data: self.root.update(copy.deepcopy(i)) - def load(self, paths=None, update=False): + def load(self, paths=None, update=False, lock=False): if self.filename is not None: - with h5py.File(self.filename, 'r') as h5file: - data = Dict(recursively_load(h5file, '/', self.inverse_transform, paths)) - if update: - self.root.update(data) - else: - self.root = data + + if lock is True: + lock_file = filelock.FileLock(self.filename + '.lock') + else: + lock_file = contextlib.nullcontext() + + with lock_file: + with h5py.File(self.filename, 'r') as h5file: + data = Dict(recursively_load(h5file, '/', self.inverse_transform, paths)) + if update: + self.root.update(data) + else: + self.root = data else: print('Filename must be set before load can be used') - def save(self): + def save(self, lock=False): if self.filename is not None: - with h5py.File(self.filename, 'w') as h5file: - recursively_save(h5file, '/', self.root, self.transform) + if lock is True: + lock_file = filelock.FileLock(self.filename + '.lock') + else: + lock_file = contextlib.nullcontext() + + with lock_file: + with h5py.File(self.filename, 'w') as h5file: + recursively_save(h5file, '/', self.root, self.transform) else: print("Filename must be set before save can be used") @@ -59,11 +75,17 @@ def load_json(self, filename, update=False): else: self.root = data - def append(self): + def append(self, lock=False): "This can only be used to write new keys to the system, this is faster than having to read the data before writing it" if self.filename is not None: - with h5py.File(self.filename, 'a') as h5file: - recursively_save(h5file, '/', self.root, self.transform) + if lock is True: + lock_file = filelock.FileLock(self.filename + '.lock') + else: + lock_file = contextlib.nullcontext() + + with lock_file: + with h5py.File(self.filename, 'a') as h5file: + recursively_save(h5file, '/', self.root, self.transform) else: print("Filename must be set before save can be used") diff --git a/setup.py b/setup.py index ee1641b..f088fc4 100644 --- a/setup.py +++ b/setup.py @@ -5,7 +5,7 @@ setuptools.setup( name="CADET", - version="0.7", + version="0.8", author="William Heymann", author_email="w.heymann@fz-juelich.de", description="CADET is a python interface to the CADET chromatography simulator", @@ -16,12 +16,13 @@ install_requires=[ 'addict', 'numpy', - 'h5py' + 'h5py', + 'filelock' ], classifiers=[ "Programming Language :: Python :: 3", "License :: OSI Approved :: BSD License", "Operating System :: OS Independent", ], - python_requires='>=3.6', + python_requires='>=3.7', )