Skip to content

Commit

Permalink
jupyter investigation to close any open h5 file handles so not to reu…
Browse files Browse the repository at this point in the history
…se old data
  • Loading branch information
PhilipDeegan committed Apr 12, 2021
1 parent 65eda4c commit 9b54bf9
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 1 deletion.
24 changes: 24 additions & 0 deletions pyphare/pyphare/core/ipython.py
Original file line number Diff line number Diff line change
@@ -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!')


2 changes: 1 addition & 1 deletion pyphare/pyphare/core/phare_utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
"""
Expand Down
21 changes: 21 additions & 0 deletions pyphare/pyphare/pharesee/hierarchy.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
"""
Expand Down Expand Up @@ -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"])
Expand Down

0 comments on commit 9b54bf9

Please sign in to comment.