Skip to content

Commit

Permalink
stack_multiprocess: fix race-condition where output files could overw…
Browse files Browse the repository at this point in the history
…rite each other

- bump to 1.0.10
  • Loading branch information
smathot committed Nov 22, 2023
1 parent 0423172 commit 1f20c2e
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 4 deletions.
2 changes: 1 addition & 1 deletion datamatrix/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@
from datamatrix._datamatrix._nifticolumn import NiftiColumn
from datamatrix._datamatrix._datamatrix import DataMatrix

__version__ = '1.0.9'
__version__ = '1.0.10'
NAN = float('nan')
INF = float('inf')
7 changes: 4 additions & 3 deletions datamatrix/functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,8 @@ def get_dm(i):
logger.debug('starting multiprocessing')
pool = mp.Pool(processes)
fnc = functools.partial(_stack_multiprocess_inner, fnc)
results = [pool.apply_async(fnc, (arg,)) for arg in args]
results = [pool.apply_async(fnc, (arg, i))
for (i, arg) in enumerate(args)]
paths = []
for result in results:
try:
Expand Down Expand Up @@ -422,7 +423,7 @@ def _count_unbound_arguments(fnc):
return len(getargspec(fnc).args) - nbound


def _stack_multiprocess_inner(fnc, arg):
def _stack_multiprocess_inner(fnc, arg, process_nr):
"""A helper function for stack_multiprocess that calls another function,
ensures that the result value is a DataMatrix, saves the DataMatrix to
disk, and then returns the path to the saved file.
Expand All @@ -435,7 +436,7 @@ def _stack_multiprocess_inner(fnc, arg):
if not isinstance(dm, DataMatrix):
raise ValueError('function should return DataMatrix, not {}'
.format(type(dm)))
path = Path(cfg.tmp_dir) / Path(f'.{id(dm)}-{os.getpid()}.dm')
path = Path(cfg.tmp_dir) / Path(f'.{id(dm)}-{os.getpid()}-{process_nr}.dm')
logger.info('writing process result to temporary file {}'.format(path))
io.writebin(dm, path)
del dm
Expand Down

0 comments on commit 1f20c2e

Please sign in to comment.