Skip to content

Commit b12ed32

Browse files
committed
Parallelize spell check
1 parent 973241b commit b12ed32

File tree

1 file changed

+32
-15
lines changed

1 file changed

+32
-15
lines changed

spellcheck.py

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#!/usr/bin/env python3
22

33
import argparse
4+
import concurrent.futures
45
import os
56
import re
67
import subprocess
@@ -98,21 +99,35 @@ def main():
9899
num_files_with_errors = 0
99100
num_files_with_processing_errors = 0
100101

101-
for notebook in args.notebooks:
102-
num_files_processed += 1
103-
error_output = check_spelling(notebook)
104-
if error_output:
105-
all_errors.append(error_output)
106-
if (
107-
"Error running command" in error_output
108-
or "An unexpected error occurred" in error_output
109-
or "Error - File not found" in error_output
110-
):
102+
futures = []
103+
with concurrent.futures.ProcessPoolExecutor() as executor:
104+
for notebook in args.notebooks:
105+
futures.append(executor.submit(check_spelling, notebook))
106+
107+
for future in concurrent.futures.as_completed(futures):
108+
num_files_processed += 1
109+
try:
110+
error_output = future.result()
111+
if error_output:
112+
all_errors.append(error_output)
113+
if (
114+
"Error running command" in error_output
115+
or "An unexpected error occurred" in error_output
116+
or "Error - File not found" in error_output
117+
):
118+
num_files_with_processing_errors += 1
119+
else:
120+
num_files_with_errors += 1
121+
except Exception as exc:
122+
print(f"An unexpected error occurred processing a task: {exc}", file=sys.stderr)
111123
num_files_with_processing_errors += 1
112-
else:
113-
num_files_with_errors += 1
124+
all_errors.append(
125+
f"**Unknown File**: An unexpected error occurred during processing:\n```\n{exc}\n```"
126+
)
114127

115128
if all_errors:
129+
all_errors.sort()
130+
116131
print("## Spell Check Report\n")
117132
print("\n\n---\n\n".join(all_errors))
118133

@@ -123,13 +138,15 @@ def main():
123138
summary_lines.append(
124139
f"Encountered processing errors in {num_files_with_processing_errors} file(s)."
125140
)
126-
if not summary_lines:
141+
if not summary_lines and all_errors:
127142
summary_lines.append(f"Found issues in {len(all_errors)} file(s).")
128143

129-
print(f"\n---\nChecked {num_files_processed} notebook(s). " + " ".join(summary_lines))
144+
total_notebooks_input = len(args.notebooks)
145+
print(f"\n---\nChecked {total_notebooks_input} notebook(s). " + " ".join(summary_lines))
130146
sys.exit(1)
131147
else:
132-
print(f"Spell check passed successfully for {num_files_processed} notebook(s).")
148+
total_notebooks_input = len(args.notebooks)
149+
print(f"Spell check passed successfully for {total_notebooks_input} notebook(s).")
133150
sys.exit(0)
134151

135152

0 commit comments

Comments
 (0)