Skip to content

Commit 9b6f505

Browse files
committed
Fix error in batch_run
1 parent 8830e1d commit 9b6f505

File tree

2 files changed

+30
-32
lines changed

2 files changed

+30
-32
lines changed

gbmi/utils/images.py

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -107,40 +107,42 @@ def batch_run(
107107
for i in range(0, len(images), batchsize)
108108
]
109109

110-
process = subprocess.Popen(
111-
[*args, *map(str, images), *post_args],
112-
stdout=subprocess.PIPE,
113-
stderr=subprocess.PIPE,
114-
**kwargs,
115-
)
116-
117110
if stdout_write is None:
118111
stdout_write = partial(print, file=sys.stdout)
119112
if stderr_write is None:
120113
stderr_write = partial(print, file=sys.stderr)
121114

122-
stdout_thread = threading.Thread(
123-
target=forward_output, args=(process.stdout, stdout_write, trim_printout)
124-
)
125-
stderr_thread = threading.Thread(
126-
target=forward_output, args=(process.stderr, stderr_write, trim_printout)
127-
)
115+
try:
116+
process = subprocess.Popen(
117+
[*args, *map(str, images), *post_args],
118+
stdout=subprocess.PIPE,
119+
stderr=subprocess.PIPE,
120+
**kwargs,
121+
)
122+
123+
stdout_thread = threading.Thread(
124+
target=forward_output, args=(process.stdout, stdout_write, trim_printout)
125+
)
126+
stderr_thread = threading.Thread(
127+
target=forward_output, args=(process.stderr, stderr_write, trim_printout)
128+
)
128129

129-
stdout_thread.start()
130-
stderr_thread.start()
130+
stdout_thread.start()
131+
stderr_thread.start()
131132

132-
stdout_thread.join()
133-
stderr_thread.join()
133+
stdout_thread.join()
134+
stderr_thread.join()
134135

135-
process.wait()
136+
process.wait()
136137

137-
if check and process.returncode != 0:
138-
exn = subprocess.CalledProcessError(process.returncode, process.args)
138+
if check and process.returncode != 0:
139+
raise subprocess.CalledProcessError(process.returncode, process.args)
140+
except (FileNotFoundError, subprocess.CalledProcessError, OSError) as e:
139141
if wrap_errs is not None:
140-
stderr_write(f"Error: {exn}")
141-
wrap_errs.append(exn)
142+
stderr_write(f"Error: {e}")
143+
wrap_errs.append(e)
142144
else:
143-
raise exn
145+
raise e
144146

145147
return process
146148

notebooks_jason/max_of_K_all_models.py

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -300,13 +300,7 @@ def wrap_err(f, *args, return_bool: bool = False, **kwargs):
300300
try:
301301
result = f(*args, **kwargs)
302302
return True if return_bool else result
303-
except FileNotFoundError as e:
304-
print(f"Warning: {e}")
305-
errs.append(e)
306-
except subprocess.CalledProcessError as e:
307-
print(f"Warning: {e}")
308-
errs.append(e)
309-
except OSError as e:
303+
except (FileNotFoundError, subprocess.CalledProcessError, OSError) as e:
310304
print(f"Warning: {e}")
311305
errs.append(e)
312306
if return_bool:
@@ -330,13 +324,15 @@ def optimize_pngs(errs: list[Exception] = []):
330324
# wrap_err(image_utils.optipng, f)
331325

332326
new_errs = []
333-
image_utils.optimize(
327+
opt_success = wrap_err(
328+
image_utils.optimize,
334329
*LATEX_FIGURE_PATH.glob("*.png"),
335330
exhaustive=True,
336331
trim_printout=COMPACT_IMAGE_OPTIMIZE_OUTPUT,
337332
wrap_errs=new_errs,
333+
return_bool=True,
338334
)
339-
opt_success = not new_errs
335+
opt_success = opt_success and not new_errs
340336
errs.extend(new_errs)
341337

342338
if not opt_success:

0 commit comments

Comments
 (0)