From 30d1385599beefc8f65f3c71cbbb52666bf1420b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dani=C3=ABl=20van=20Noord?= <13665637+DanielNoord@users.noreply.github.com> Date: Thu, 30 Dec 2021 17:49:02 +0100 Subject: Use ``with`` statement in ``parallel.py`` (#5612) --- pylint/lint/parallel.py | 10 +++------- tests/test_check_parallel.py | 6 +++--- 2 files changed, 6 insertions(+), 10 deletions(-) diff --git a/pylint/lint/parallel.py b/pylint/lint/parallel.py index 6dbeed983..b382c42ad 100644 --- a/pylint/lint/parallel.py +++ b/pylint/lint/parallel.py @@ -133,11 +133,10 @@ def check_parallel( # is identical to the linter object here. This is required so that # a custom PyLinter object can be used. initializer = functools.partial(_worker_initialize, arguments=arguments) - pool = multiprocessing.Pool( # pylint: disable=consider-using-with + with multiprocessing.Pool( jobs, initializer=initializer, initargs=[dill.dumps(linter)] - ) - linter.open() - try: + ) as pool: + linter.open() all_stats = [] all_mapreduce_data = collections.defaultdict(list) @@ -164,9 +163,6 @@ def check_parallel( all_stats.append(stats) all_mapreduce_data[worker_idx].append(mapreduce_data) linter.msg_status |= msg_status - finally: - pool.close() - pool.join() _merge_mapreduce_data(linter, all_mapreduce_data) linter.stats = merge_stats([linter.stats] + all_stats) diff --git a/tests/test_check_parallel.py b/tests/test_check_parallel.py index 4fa49d148..bc27530f3 100644 --- a/tests/test_check_parallel.py +++ b/tests/test_check_parallel.py @@ -189,10 +189,10 @@ class TestCheckParallelFramework: """ linter = PyLinter(reporter=Reporter()) linter.attribute = argparse.ArgumentParser() # type: ignore[attr-defined] - pool = multiprocessing.Pool( # pylint: disable=consider-using-with + with multiprocessing.Pool( 2, initializer=worker_initialize, initargs=[dill.dumps(linter)] - ) - pool.imap_unordered(print, [1, 2]) + ) as pool: + pool.imap_unordered(print, [1, 2]) def test_worker_check_single_file_uninitialised(self) -> None: pylint.lint.parallel._worker_linter = None -- cgit v1.2.1