Skip to content

Commit

Permalink
proposed solution for #154 but not straight-forward to test
Browse files Browse the repository at this point in the history
  • Loading branch information
paulmueller committed Jan 24, 2022
1 parent 255c7ec commit c50efd2
Showing 1 changed file with 42 additions and 1 deletion.
43 changes: 42 additions & 1 deletion dclab/rtdc_dataset/fmt_hdf5.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,47 @@ class UnknownKeyWarning(UserWarning):
pass


class H5FileWapper:
def __init__(self, path):
"""Wrapper around :class:`h5py.File` that enables reopening
Sometimes, when you are working with data on a network share,
you get temporary disconnects and then h5py might complain
with an OSError (e.g. Windows "[Error 22] Can't read data").
This wrapper tries to circumvent this issue by catching those
exceptions and trying to re-open the file.
These errors might get
- Linux: KeyError: "Unable to open object
"""
self.h5 = h5py.File(path, mode="r")
self.path = pathlib.Path(path)

@property
def attrs(self):
try:
return self.h5.attrs
except BaseException:
self.assert_h5_available()
return self.h5.attrs

def assert_h5_available(self):
if self.path.exists():
try:
self.h5.close()
except RuntimeError:
pass
self.h5 = h5py.File(self.path, mode="r")

def __getitem__(self, item):
try:
return self.h5[item]
except BaseException:
self.assert_h5_available()
return self.h5[item]


class H5ContourEvent:
def __init__(self, h5group):
self.h5group = h5group
Expand Down Expand Up @@ -221,7 +262,7 @@ def __init__(self, h5path, *args, **kwargs):
self.path = h5path

# Setup events
self._h5 = h5py.File(h5path, mode="r")
self._h5 = H5FileWapper(h5path)
self._events = H5Events(self._h5)

# Parse configuration
Expand Down

0 comments on commit c50efd2

Please sign in to comment.