From 9b54bf9ae77b887d5e5dda9f82703cb312db44de Mon Sep 17 00:00:00 2001 From: dekken Date: Mon, 12 Apr 2021 13:44:11 +0200 Subject: [PATCH] jupyter investigation to close any open h5 file handles so not to reuse old data --- pyphare/pyphare/core/ipython.py | 24 ++++++++++++++++++++++++ pyphare/pyphare/core/phare_utilities.py | 2 +- pyphare/pyphare/pharesee/hierarchy.py | 21 +++++++++++++++++++++ 3 files changed, 46 insertions(+), 1 deletion(-) create mode 100644 pyphare/pyphare/core/ipython.py diff --git a/pyphare/pyphare/core/ipython.py b/pyphare/pyphare/core/ipython.py new file mode 100644 index 000000000..a8fcb1e3e --- /dev/null +++ b/pyphare/pyphare/core/ipython.py @@ -0,0 +1,24 @@ + +# https://stackoverflow.com/a/40222538/795574 + +# exit_register runs at the end of ipython %run or the end of the python interpreter +try: + def exit_register(fun, *args, **kwargs): + """ Decorator that registers at post_execute. After its execution it + unregisters itself for subsequent runs. """ + def callback(): + fun() + ip.events.unregister('post_execute', callback) + ip.events.register('post_execute', callback) + + + ip = get_ipython() +except NameError: + from atexit import register as exit_register + + +# @exit_register +# def callback(): +# print('I\'m done!') + + diff --git a/pyphare/pyphare/core/phare_utilities.py b/pyphare/pyphare/core/phare_utilities.py index 6863e0251..181f40b1f 100644 --- a/pyphare/pyphare/core/phare_utilities.py +++ b/pyphare/pyphare/core/phare_utilities.py @@ -115,7 +115,7 @@ def decode_bytes(input, errors="ignore"): return input.decode("ascii", errors=errors) -def run_cli_cmd(cmd, shell=True, capture_output=True, check=False, print_cmd=True): +def run_cli_cmd(cmd, shell=True, capture_output=True, check=False, print_cmd=False): """ https://docs.python.org/3/library/subprocess.html """ diff --git a/pyphare/pyphare/pharesee/hierarchy.py b/pyphare/pyphare/pharesee/hierarchy.py index c01b07e01..cb61d69e8 100644 --- a/pyphare/pyphare/pharesee/hierarchy.py +++ b/pyphare/pyphare/pharesee/hierarchy.py @@ -5,12 +5,32 @@ from .particles import Particles +from ..core.ipython import exit_register from ..core import box as boxm from ..core.box import Box from ..core.gridlayout import GridLayout import matplotlib.pyplot as plt from ..core.phare_utilities import np_array_ify, is_scalar, listify, refinement_ratio +import weakref +h5_weak_refs = {} + +def clean_dead_h5_weak_refs(): + for object_id, h5_weak_ref in h5_weak_refs.copy().items(): + if h5_weak_ref() is None: + del h5_weak_refs[object_id] + +def add_h5_weak_ref(h5File): + clean_dead_h5_weak_refs() + h5_weak_refs[id(h5File)] = weakref.ref(h5File) + +@exit_register +def close_h5_files_refs(): + for h5_weak_ref in list(h5_weak_refs.values()): + if h5_weak_ref() is not None and h5_weak_ref().__bool__(): + h5_weak_ref().close() + + class PatchData: """ @@ -859,6 +879,7 @@ def patch_has_datasets(h5_patch_grp): def hierarchy_fromh5(h5_filename, time, hier, silent=True): import h5py data_file = h5py.File(h5_filename, "r") + add_h5_weak_ref(data_file) basename = os.path.basename(h5_filename) root_cell_width = np.asarray(data_file.attrs["cell_width"])