Skip to content

Commit

Permalink
properly close resources
Browse files Browse the repository at this point in the history
  • Loading branch information
magland committed Mar 21, 2024
1 parent 76925be commit a2cab5f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 12 deletions.
22 changes: 11 additions & 11 deletions lindi/LindiH5ZarrStore/LindiH5ZarrStore.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,17 @@ def __init__(
_file: Union[IO, Any],
_opts: LindiH5ZarrStoreOpts,
_url: Union[str, None] = None,
_entities_to_close: List[Any]
):
"""
Do not call the constructor directly. Instead, use the from_file class
method.
"""
self._file = _file
self._h5f = h5py.File(_file, "r")
self._file: Union[IO, Any, None] = _file
self._h5f: Union[h5py.File, None] = h5py.File(_file, "r")
self._url = _url
self._opts = _opts
self._entities_to_close = _entities_to_close + [self._h5f]

# Some datasets do not correspond to traditional chunked datasets. For
# those datasets, we need to store the inline data so that we can return
Expand Down Expand Up @@ -97,21 +99,19 @@ def from_file(
if hdf5_file_name_or_url.startswith(
"http://"
) or hdf5_file_name_or_url.startswith("https://"):
# note that the remfile.File object does not need to be closed
remf = remfile.File(hdf5_file_name_or_url, verbose=False)
return LindiH5ZarrStore(_file=remf, _url=hdf5_file_name_or_url, _opts=opts)
return LindiH5ZarrStore(_file=remf, _url=hdf5_file_name_or_url, _opts=opts, _entities_to_close=[])
else:
f = open(hdf5_file_name_or_url, "rb")
return LindiH5ZarrStore(_file=f, _url=url, _opts=opts)
return LindiH5ZarrStore(_file=f, _url=url, _opts=opts, _entities_to_close=[f])

def close(self):
"""Close the store."""
if hasattr(self, "_h5f") and self._h5f:
self._h5f.close()
self._h5f = None
if hasattr(self, "_file") and self._file:
if not isinstance(self._file, remfile.File):
self._file.close()
self._file = None
for e in self._entities_to_close:
e.close()
self._h5f = None
self._file = None

def __getitem__(self, key):
"""Get an item from the store (required by base class)."""
Expand Down
4 changes: 3 additions & 1 deletion lindi/LindiH5pyFile/LindiH5pyFile.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ def from_zarr_store(zarr_store: Union[ZarrStore, FSMap]):
"""
Create a LindiH5pyFile from a zarr store.
"""
# note that even though the function is called "open", the zarr_group
# does not need to be closed
zarr_group = zarr.open(store=zarr_store, mode="r")
assert isinstance(zarr_group, zarr.Group)
return LindiH5pyFile.from_zarr_group(zarr_group)
Expand Down Expand Up @@ -122,7 +124,7 @@ def swmr_mode(self, value): # type: ignore
raise Exception("Getting swmr_mode is not allowed")

def close(self):
# return self._file_object.close()
# Nothing was opened, so nothing needs to be closed
pass

def flush(self):
Expand Down

0 comments on commit a2cab5f

Please sign in to comment.