-
Notifications
You must be signed in to change notification settings - Fork 24
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
jupyter investigation to close any open h5 file handles so not to reuse old data #527
Draft
PhilipDeegan
wants to merge
1
commit into
PHAREHUB:master
Choose a base branch
from
PhilipDeegan:ipython
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
|
||
|
||
|
||
|
||
import weakref | ||
h5_weak_refs = {} | ||
|
||
def clean_dead_h5_weak_refs(): | ||
for h5_path, h5_weak_ref in h5_weak_refs.copy().items(): | ||
if h5_weak_ref["ref"]() is None: | ||
del h5_weak_refs[h5_path] | ||
|
||
# https://stackoverflow.com/a/40222538/795574 | ||
try: | ||
def exit_register(fun, *args, **kwargs): | ||
def callback(): | ||
fun() | ||
ip.events.unregister('post_execute', callback) | ||
ip.events.register('post_execute', callback) | ||
|
||
ip = get_ipython() # should fail if not running under ipython | ||
|
||
def add_h5_weak_ref(h5Filepath, h5File): | ||
import pathlib, datetime, time | ||
clean_dead_h5_weak_refs() | ||
|
||
h5_weak_refs[h5Filepath] = { | ||
"ref": weakref.ref(h5File), | ||
"access": int(time.time()), | ||
"path": pathlib.Path(h5Filepath) | ||
} | ||
|
||
except NameError: | ||
|
||
def add_h5_weak_ref(h5Filepath, h5File): | ||
pass # empty function for normal (not jupyter) case | ||
|
||
# forwarding import to keep functionality even if ipython is not in use | ||
# it's possible this isn't even needed, and we just want to execute the callback for only if | ||
# we're running under ipython, in that case, "exit_register" -> "ipython_exit_register" and | ||
# replace the next line with def ipython_exit_register(fun, *args, **kwargs): pass | ||
from atexit import register as exit_register # lgtm [py/unused-import] | ||
|
||
|
||
|
||
|
||
@exit_register | ||
def close_h5_files_refs(): | ||
import pathlib, datetime, time | ||
for h5_weak_ref_dict in list(h5_weak_refs.values()): | ||
h5_ref = h5_weak_ref_dict["ref"]() | ||
if h5_ref is not None: | ||
mtime = int(h5_weak_ref_dict["path"].stat().st_mtime) | ||
if mtime > h5_weak_ref_dict["access"] and h5_ref.__bool__(): | ||
h5_ref.close() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
probably not indented correclty?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's been some time since looking at this, and I'm pretty sure I had another later branch with modifications which I never added so it's quite likely