Skip to content

Commit 2429a0b

Browse files
committed
jupyter investigation to close any open h5 file handles so not to reuse old data
1 parent 65eda4c commit 2429a0b

File tree

3 files changed

+56
-1
lines changed

3 files changed

+56
-1
lines changed

pyphare/pyphare/core/ipython.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
2+
# https://stackoverflow.com/a/40222538/795574
3+
4+
# exit_register runs at the end of ipython %run or the end of the python interpreter
5+
try:
6+
def exit_register(fun, *args, **kwargs):
7+
""" Decorator that registers at post_execute. After its execution it
8+
unregisters itself for subsequent runs. """
9+
def callback():
10+
fun()
11+
ip.events.unregister('post_execute', callback)
12+
ip.events.register('post_execute', callback)
13+
14+
15+
ip = get_ipython()
16+
except NameError:
17+
from atexit import register as exit_register
18+
19+
20+
# @exit_register
21+
# def callback():
22+
# print('I\'m done!')
23+
24+

pyphare/pyphare/core/phare_utilities.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ def decode_bytes(input, errors="ignore"):
115115
return input.decode("ascii", errors=errors)
116116

117117

118-
def run_cli_cmd(cmd, shell=True, capture_output=True, check=False, print_cmd=True):
118+
def run_cli_cmd(cmd, shell=True, capture_output=True, check=False, print_cmd=False):
119119
"""
120120
https://docs.python.org/3/library/subprocess.html
121121
"""

pyphare/pyphare/pharesee/hierarchy.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,42 @@
55

66
from .particles import Particles
77

8+
from ..core.ipython import exit_register
89
from ..core import box as boxm
910
from ..core.box import Box
1011
from ..core.gridlayout import GridLayout
1112
import matplotlib.pyplot as plt
1213
from ..core.phare_utilities import np_array_ify, is_scalar, listify, refinement_ratio
1314

15+
import weakref
16+
h5_weak_refs = {}
17+
18+
def add_h5_weak_ref(h5File):
19+
# tmp = h5_weak_refs.copy()
20+
# for object_id, h5_weak_ref in h5_weak_refs:
21+
# if not h5_weak_ref.alive():
22+
# del tmp[object_id]
23+
# h5_weak_refs = tmp
24+
25+
object_id = id(h5File)
26+
#h5_weak_refs[object_id] = weakref.ref(h5File)
27+
h5_weak_refs[object_id] = h5File
28+
29+
@exit_register
30+
def close_h5_files():
31+
for h5File in list(h5_weak_refs.values()):
32+
if h5File.__bool__():
33+
# print("closing", h5File)
34+
h5File.close()
35+
36+
# @exit_register
37+
def close_h5_files_refs():
38+
for object_id, h5_weak_ref in h5_weak_refs.items():
39+
if h5_weak_ref() is not None and h5_weak_ref().__bool__():
40+
# print("closing", h5_weak_ref())
41+
h5_weak_ref().close()
42+
43+
1444

1545
class PatchData:
1646
"""
@@ -859,6 +889,7 @@ def patch_has_datasets(h5_patch_grp):
859889
def hierarchy_fromh5(h5_filename, time, hier, silent=True):
860890
import h5py
861891
data_file = h5py.File(h5_filename, "r")
892+
add_h5_weak_ref(data_file)
862893
basename = os.path.basename(h5_filename)
863894

864895
root_cell_width = np.asarray(data_file.attrs["cell_width"])

0 commit comments

Comments
 (0)