From 9ad39a8808a8a859683cee1d4f817ea92461b248 Mon Sep 17 00:00:00 2001 From: Robert Smith Date: Sat, 17 Aug 2024 12:08:07 +1000 Subject: [PATCH 1/2] mrtrix3.path.wait_for(): Fix variable ghosting --- lib/mrtrix3/path.py | 49 ++++++++++++++++++++++++++------------------- 1 file changed, 28 insertions(+), 21 deletions(-) diff --git a/lib/mrtrix3/path.py b/lib/mrtrix3/path.py index 53c7e8a8cf..ef2d7015b9 100644 --- a/lib/mrtrix3/path.py +++ b/lib/mrtrix3/path.py @@ -217,7 +217,7 @@ def in_use(path): # A fatal error will result in a non-zero code -> in_use() = False, so wait_for() can return return not subprocess.call(['fuser', '-s', path], shell=False, stdin=None, stdout=None, stderr=None) - def num_exit(data): + def num_exist(data): count = 0 for entry in data: if os.path.exists(entry): @@ -249,21 +249,25 @@ def num_in_use(data): app.debug(str(paths)) # Wait until all files exist - num_exist = num_exit(paths) - if num_exist != len(paths): - progress = app.ProgressBar('Waiting for creation of ' + (('new item \"' + paths[0] + '\"') if len(paths) == 1 else (str(len(paths)) + ' new items')), len(paths)) - for _ in range(num_exist): + current_num_exist = num_exist(paths) + if current_num_exist != len(paths): + progress = app.ProgressBar('Waiting for creation of ' \ + + (('new item \"' + paths[0] + '\"') \ + if len(paths) == 1 \ + else (str(len(paths)) + ' new items')), + len(paths)) + for _ in range(current_num_exist): progress.increment() delay = 1.0/1024.0 - while not num_exist == len(paths): + while not current_num_exist == len(paths): time.sleep(delay) - new_num_exist = num_exit(paths) - if new_num_exist == num_exist: + new_num_exist = num_exist(paths) + if new_num_exist == current_num_exist: delay = max(60.0, delay*2.0) - elif new_num_exist > num_exist: - for _ in range(new_num_exist - num_exist): + elif new_num_exist > current_num_exist: + for _ in range(new_num_exist - current_num_exist): progress.increment() - num_exist = new_num_exist + current_num_exist = new_num_exist delay = 1.0/1024.0 progress.done() else: @@ -280,28 +284,31 @@ def num_in_use(data): return # Can we query the in-use status of any of these paths - num_in_use = num_in_use(paths) - if num_in_use is None: + current_num_in_use = num_in_use(paths) + if current_num_in_use is None: app.debug('Unable to test for finalization of new files') return # Wait until all files are not in use - if not num_in_use: + if not current_num_in_use: app.debug('Item' + ('s' if len(paths) > 1 else '') + ' immediately ready') return - progress = app.ProgressBar('Waiting for finalization of ' + (('new file \"' + paths[0] + '\"') if len(paths) == 1 else (str(len(paths)) + ' new files'))) - for _ in range(len(paths) - num_in_use): + progress = app.ProgressBar('Waiting for finalization of ' + + (('new file \"' + paths[0] + '\"') \ + if len(paths) == 1 \ + else (str(len(paths)) + ' new files'))) + for _ in range(len(paths) - current_num_in_use): progress.increment() delay = 1.0/1024.0 - while num_in_use: + while current_num_in_use: time.sleep(delay) new_num_in_use = num_in_use(paths) - if new_num_in_use == num_in_use: + if new_num_in_use == current_num_in_use: delay = max(60.0, delay*2.0) - elif new_num_in_use < num_in_use: - for _ in range(num_in_use - new_num_in_use): + elif new_num_in_use < current_num_in_use: + for _ in range(current_num_in_use - new_num_in_use): progress.increment() - num_in_use = new_num_in_use + current_num_in_use = new_num_in_use delay = 1.0/1024.0 progress.done() From e866aa42a6fec605fa2635b0df5976d89e5a8fb0 Mon Sep 17 00:00:00 2001 From: Robert Smith Date: Sat, 17 Aug 2024 12:36:18 +1000 Subject: [PATCH 2/2] mrtrix3.path.wait_for(): Fix sleep timing --- lib/mrtrix3/path.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/mrtrix3/path.py b/lib/mrtrix3/path.py index ef2d7015b9..dba7482f7c 100644 --- a/lib/mrtrix3/path.py +++ b/lib/mrtrix3/path.py @@ -263,7 +263,7 @@ def num_in_use(data): time.sleep(delay) new_num_exist = num_exist(paths) if new_num_exist == current_num_exist: - delay = max(60.0, delay*2.0) + delay = min(60.0, delay*2.0) elif new_num_exist > current_num_exist: for _ in range(new_num_exist - current_num_exist): progress.increment() @@ -305,7 +305,7 @@ def num_in_use(data): time.sleep(delay) new_num_in_use = num_in_use(paths) if new_num_in_use == current_num_in_use: - delay = max(60.0, delay*2.0) + delay = min(60.0, delay*2.0) elif new_num_in_use < current_num_in_use: for _ in range(current_num_in_use - new_num_in_use): progress.increment()