Skip to content

Commit fef6843

Browse files
authored
Switch from Multi Threading to Multi Processing (MetOffice#206)
* Yet another weird round of having to repeatedly accept the same changes which git / VS Code are marking as in conflict. Annoyingly, this seems to have introduced a bug as well. * Fixing issue with external runners not establishing properly. * Fixing an error that seems to have crept in during merging main and resolving clashes. But looking at it, I really can't fathom out 'how' * Where do these line length errors in the linter(s) come from - I haven't edited this file... * quick "improvement" to the error reporting in one of the tests * Why am I constantly having to re-impliment fixes/tidying I'm sure I've done before. * And now ruff_format wants to change them back again... * Fix issue with "TLD" being repeated for paths when using --fullcheck * Undoing auto formatting again as github CI doesn't agree with it (picard.gif) * Remove the thread lock (not threading any more) to allow TestResult to be pickled * 1st draft switching to multiprocessing. * Functioning multiprocessor version using ProcessPoolExecuter * Tidy and reduce default max-workers to 2 * Remove extraneous print call that merging main seems to have reinstated.
1 parent ef0217d commit fef6843

2 files changed

Lines changed: 11 additions & 21 deletions

File tree

script_umdp3_checker/umdp3_checker_rules.py

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
"""
1111

1212
import re
13-
import threading
1413
from typing import List, Dict
1514
from fortran_keywords import fortran_keywords
1615
from search_lists import (
@@ -65,7 +64,6 @@ class UMDP3Checker:
6564

6665
def __init__(self):
6766
self._extra_error_info = {}
68-
self._lock = threading.Lock()
6967
"""
7068
TODO: The Perl version had a dodgy looking subroutine to calculate
7169
this, but I can't find where it was called from within the files in
@@ -81,8 +79,7 @@ def reset_extra_error_information(self):
8179
Appears to be used 'between' blocks of tests such as those on diffs and
8280
those on full files.
8381
"""
84-
with self._lock:
85-
self._extra_error_info = {}
82+
self._extra_error_info = {}
8683

8784
def get_extra_error_information(self) -> Dict:
8885
"""
@@ -94,16 +91,14 @@ def get_extra_error_information(self) -> Dict:
9491
actual failures and not just the count. However, this information
9592
doesn't seem to be output as yet and will need implementing.
9693
"""
97-
with self._lock:
98-
return self._extra_error_info.copy()
94+
return self._extra_error_info.copy()
9995

10096
def add_extra_error(self, key: str, value: str = ""):
10197
"""Add extra error information to the dictionary"""
10298
"""
10399
TODO: The usefulness of the information added has not been assessed,
104100
nor does it appear to be reported as yet."""
105-
with self._lock:
106-
self._extra_error_info[key] = value
101+
self._extra_error_info[key] = value
107102

108103
def add_error_log(
109104
self, error_log: Dict, key: str = "no key", value: int = 0
@@ -188,7 +183,6 @@ def capitulated_keywords(self, lines: List[str]) -> TestResult:
188183
failure_count=failures,
189184
passed=(failures == 0),
190185
output=f"Checked {line_count} lines, found {failures} failures.",
191-
# errors=self.get_extra_error_information()
192186
errors=error_log,
193187
)
194188

script_umdp3_checker/umdp3_conformance.py

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -202,11 +202,6 @@ def create_external_runners(
202202
) -> "StyleChecker":
203203
"""Create a StyleChecker instance filtering files from a full list."""
204204
filtered_files = cls.filter_files(all_files, file_extensions)
205-
print(
206-
f"Creating external runners for {name} with {len(commands)} "
207-
f"commands and {len(filtered_files)} files to check from a "
208-
f"total of {len(all_files)} files."
209-
)
210205
check_functions = {}
211206
for command in commands:
212207
external_opname = f"External_operation_{command[0]}"
@@ -285,7 +280,7 @@ class ConformanceChecker:
285280
def __init__(
286281
self,
287282
checkers: List[StyleChecker],
288-
max_workers: int = 8,
283+
max_workers: int = 2,
289284
):
290285
self.checkers = checkers
291286
self.max_workers = max_workers
@@ -324,15 +319,16 @@ def check_files(self) -> None:
324319
then have each worker run all the checks for a given file. This would
325320
reduce the overhead of creating threads and allow for better use of
326321
resources."""
327-
with concurrent.futures.ThreadPoolExecutor(
322+
with concurrent.futures.ProcessPoolExecutor(
328323
max_workers=self.max_workers
329324
) as executor:
330325
future_to_task = {
331326
executor.submit(checker.check, file_path): file_path
332327
for checker in self.checkers
333328
for file_path in checker.files_to_check
334329
}
335-
330+
# TODO : This next loop could be used to process the individual results as
331+
# they come in, rather than waiting for all to complete.
336332
for future in concurrent.futures.as_completed(future_to_task):
337333
result = future.result()
338334
results.append(result)
@@ -403,7 +399,7 @@ def process_arguments():
403399
"-p", "--path", type=str, default="./", help="path to repository"
404400
)
405401
parser.add_argument(
406-
"--max-workers", type=int, default=8, help="Maximum number of parallel workers"
402+
"--max-workers", type=int, default=2, help="Maximum number of parallel workers"
407403
)
408404
parser.add_argument(
409405
"--fullcheck",
@@ -417,11 +413,11 @@ def process_arguments():
417413
help="Print details of passed checks as well as failed ones.\n"
418414
"By default, only failed checks are printed in detail.",
419415
)
420-
group = parser.add_mutually_exclusive_group()
421-
group.add_argument(
416+
verbosity_grp = parser.add_mutually_exclusive_group()
417+
verbosity_grp.add_argument(
422418
"-v", "--verbose", action="count", default=0, help="Increase output verbosity"
423419
)
424-
group.add_argument(
420+
verbosity_grp.add_argument(
425421
"-q", "--quiet", action="count", default=0, help="Decrease output verbosity"
426422
)
427423
# The following are not yet implemented, but may become useful

0 commit comments

Comments
 (0)