Skip to content

Commit

Permalink
Experiment with lock to avoid deadlocks
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonHeybrock committed Jan 17, 2024
1 parent 2f7a6b9 commit 097d8c9
Showing 1 changed file with 5 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/scippnexus/file.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# SPDX-License-Identifier: BSD-3-Clause
# Copyright (c) 2023 Scipp contributors (https://github.com/scipp)
# @author Simon Heybrock
import threading
from contextlib import AbstractContextManager
from typing import Mapping

Expand All @@ -12,6 +13,8 @@


class File(AbstractContextManager, Group):
_lock = threading.Lock()

def __init__(self, *args, definitions: Mapping = _default_definitions, **kwargs):
"""Context manager for NeXus files, similar to h5py.File.
Expand All @@ -30,11 +33,13 @@ def __init__(self, *args, definitions: Mapping = _default_definitions, **kwargs)
super().__init__(self._file, definitions=definitions)

def __enter__(self):
self._lock.acquire()
self._file.__enter__()
return self

def __exit__(self, exc_type, exc_value, traceback):
self._file.close()
self._lock.release()

def close(self):
self._file.close()

0 comments on commit 097d8c9

Please sign in to comment.