Skip to content

ENH: Always fsync pickle files #3083

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

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions nipype/pipeline/engine/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,17 +236,17 @@ def save_resultfile(result, cwd, name, rebase=None):

if result.outputs is None:
logger.warning('Storing result file without outputs')
savepkl(resultsfile, result)
savepkl(resultsfile, result, sync=True)
return
try:
output_names = result.outputs.copyable_trait_names()
except AttributeError:
logger.debug('Storing non-traited results, skipping rebase of paths')
savepkl(resultsfile, result)
savepkl(resultsfile, result, sync=True)
return

if not rebase:
savepkl(resultsfile, result)
savepkl(resultsfile, result, sync=True)
return

backup_traits = {}
Expand All @@ -262,7 +262,7 @@ def save_resultfile(result, cwd, name, rebase=None):
backup_traits[key] = old
val = rebase_path_traits(result.outputs.trait(key), old, cwd)
setattr(result.outputs, key, val)
savepkl(resultsfile, result)
savepkl(resultsfile, result, sync=True)
finally:
# Restore resolved paths from the outputs dict no matter what
for key, val in list(backup_traits.items()):
Expand Down
26 changes: 14 additions & 12 deletions nipype/utils/filemanip.py
Original file line number Diff line number Diff line change
Expand Up @@ -789,18 +789,20 @@ def read_stream(stream, logger=None, encoding=None):
return out.splitlines()


def savepkl(filename, record, versioning=False):
pklopen = gzip.open if filename.endswith('.pklz') else open
with SoftFileLock('%s.lock' % filename):
with pklopen(filename, 'wb') as pkl_file:
if versioning:
from nipype import __version__ as version
metadata = json.dumps({'version': version})

pkl_file.write(metadata.encode('utf-8'))
pkl_file.write('\n'.encode('utf-8'))

pickle.dump(record, pkl_file)
def savepkl(filename, record, versioning=False, sync=False):
with open(filename, 'wb') as fobj:
pkl_file = gzip.GzipFile(fileobj=fobj) if filename.endswith('.pklz') else fobj
if versioning:
from nipype import __version__ as version
metadata = json.dumps({'version': version})

pkl_file.write(metadata.encode('utf-8'))
pkl_file.write('\n'.encode('utf-8'))

pickle.dump(record, pkl_file)
if sync:
fobj.flush()
os.fsync(fobj.fileno())


rst_levels = ['=', '-', '~', '+']
Expand Down