Skip to content

Commit 7c58aad

Browse files
committed
Acquire a lock prior to top level bids file modification
Uses SoftLockFile from py-filelock.
1 parent 8cdf06d commit 7c58aad

File tree

2 files changed

+14
-8
lines changed

2 files changed

+14
-8
lines changed

heudiconv/convert.py

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from filelock import SoftFileLock
12
import os
23
import os.path as op
34
import logging
@@ -32,6 +33,8 @@
3233
compress_dicoms
3334
)
3435

36+
LOCKFILE = 'heudiconv.lock'
37+
LOCKFILE_TIMEOUT = 10
3538
lgr = logging.getLogger(__name__)
3639

3740

@@ -202,14 +205,16 @@ def prep_conversion(sid, dicoms, outdir, heuristic, converter, anon_sid,
202205
clear_temp_dicoms(item_dicoms)
203206

204207
if bids:
205-
if seqinfo:
206-
keys = list(seqinfo)
207-
add_participant_record(anon_outdir,
208-
anon_sid,
209-
keys[0].patient_age,
210-
keys[0].patient_sex)
211-
populate_bids_templates(anon_outdir,
212-
getattr(heuristic, 'DEFAULT_FIELDS', {}))
208+
with SoftFileLock(op.join(anon_outdir, LOCKFILE),
209+
timeout=LOCKFILE_TIMEOUT):
210+
if seqinfo:
211+
keys = list(seqinfo)
212+
add_participant_record(anon_outdir,
213+
anon_sid,
214+
keys[0].patient_age,
215+
keys[0].patient_sex)
216+
populate_bids_templates(anon_outdir,
217+
getattr(heuristic, 'DEFAULT_FIELDS', {}))
213218

214219

215220
def convert(items, converter, scaninfo_suffix, custom_callable, with_prov,

heudiconv/info.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
'nipype>=0.12.0',
1515
'pathlib',
1616
'dcmstack>=0.7',
17+
'filelock>=3.0.12',
1718
]
1819

1920
TESTS_REQUIRES = [

0 commit comments

Comments
 (0)