Skip to content

Commit 55455cf

Browse files
Merge pull request #98 from seibert-media/little-refactorings
parallel_runner: Use defaultdict Fix: Crashes when switching screens in parallel mode
2 parents 328527a + e7a3f3f commit 55455cf

File tree

2 files changed

+10
-12
lines changed

2 files changed

+10
-12
lines changed

automatix/parallel_runner.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import pickle
33
import subprocess
44
from argparse import Namespace
5+
from collections import defaultdict
56
from tempfile import TemporaryDirectory
67
from time import time
78

@@ -12,12 +13,10 @@
1213

1314

1415
def get_batch_groups(batch_items: list) -> dict:
15-
batch_groups = {'_default_': []}
16+
batch_groups = defaultdict(list)
1617
for batch_item in batch_items:
1718
if group := batch_item.get('group'):
1819
assert group != '_default_', 'The group name "_default_" is reserved. Please use something different.'
19-
if group not in batch_groups.keys():
20-
batch_groups[group] = []
2120
batch_groups[group].append(batch_item)
2221
else:
2322
batch_groups['_default_'].append(batch_item)
@@ -38,7 +37,7 @@ def create_auto_files(script: dict, batch_items: list, args: Namespace, tempdir:
3837
LOG.info(f'Using temporary directory to save object files: {tempdir}')
3938

4039
batch_groups = get_batch_groups(batch_items=batch_items)
41-
default_group = batch_groups.pop('_default_')
40+
default_group = batch_groups.pop('_default_', [])
4241

4342
digits = len(str(len(batch_groups) + len(default_group)))
4443

automatix/parallel_ui.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ def process_user_input(cw: CursesWriter, autos: Autos) -> str | None:
168168
cw.input_buffer += char
169169

170170

171-
def parallel_ui(stdscr: curses.window, tempdir: str):
171+
def parallel_ui(stdscr: curses.window, tempdir: str) -> tuple[str, str | None]:
172172
cw = CursesWriter(stdscr=stdscr)
173173

174174
# Wait until the status file exists
@@ -194,24 +194,23 @@ def parallel_ui(stdscr: curses.window, tempdir: str):
194194
cw.stdscr.nodelay(False)
195195
while cw.stdscr.getch() not in [ord('q'), ord('Q')]:
196196
sleep(0.1)
197-
return 'quit'
197+
return 'quit', None
198198

199199
# 4. Process user input
200200
screen_to_switch = process_user_input(cw=cw, autos=autos)
201201
if screen_to_switch:
202-
# Important: End curses before an external program takes control of the terminal
203-
curses.endwin()
204-
print(f"Switching to screen '{screen_to_switch}'... (Return with <ctrl>+a d)")
205-
subprocess.run(['screen', '-r', screen_to_switch])
206-
return 'restart'
202+
return 'restart', screen_to_switch
207203

208204
sleep(0.2) # Short pause to reduce CPU load
209205

210206

211207
def screen_switch_loop(tempdir: str):
212208
while True:
213209
try:
214-
exit_reason = curses.wrapper(parallel_ui, tempdir)
210+
exit_reason, screen_to_switch = curses.wrapper(parallel_ui, tempdir)
211+
if screen_to_switch:
212+
print(f"Switching to screen '{screen_to_switch}'... (Return with <ctrl>+a d)")
213+
subprocess.run(['screen', '-r', screen_to_switch])
215214
except curses.error as exc:
216215
LOG.error(f"Curses error: {exc}")
217216
LOG.error("Could not start the curses interface. Is the terminal compatible?")

0 commit comments

Comments
 (0)