Skip to content

Commit ae910e0

Browse files
authored
Merge pull request #2442 from DanielNoord/executor-context
Ensure `multiprocessing.Pool` is always closed and joined
2 parents 2ddf3ed + 2f48d4b commit ae910e0

File tree

1 file changed

+56
-45
lines changed

1 file changed

+56
-45
lines changed

isort/main.py

Lines changed: 56 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import os
77
import sys
88
from collections.abc import Sequence
9+
from contextlib import AbstractContextManager, nullcontext
910
from gettext import gettext as _
1011
from io import TextIOWrapper
1112
from pathlib import Path
@@ -1058,6 +1059,9 @@ def identify_imports_main(
10581059
print(str(identified_import))
10591060

10601061

1062+
# Ignore DeepSource cyclomatic complexity check for this function. It is one
1063+
# the main entrypoints so sort of expected to be complex.
1064+
# skipcq: PY-R1000
10611065
def main(argv: Sequence[str] | None = None, stdin: TextIOWrapper | None = None) -> None:
10621066
arguments = parse_args(argv)
10631067
if arguments.get("show_version"):
@@ -1193,58 +1197,65 @@ def main(argv: Sequence[str] | None = None, stdin: TextIOWrapper | None = None)
11931197
print(ASCII_ART)
11941198

11951199
if jobs:
1196-
import multiprocessing # noqa: PLC0415
1200+
import multiprocessing.pool # noqa: PLC0415
11971201

1198-
executor = multiprocessing.Pool(jobs if jobs > 0 else multiprocessing.cpu_count())
1199-
attempt_iterator = executor.imap(
1200-
functools.partial(
1201-
sort_imports,
1202-
config=config,
1203-
check=check,
1204-
ask_to_apply=ask_to_apply,
1205-
show_diff=show_diff,
1206-
write_to_stdout=write_to_stdout,
1207-
extension=ext_format,
1208-
config_trie=config_trie,
1209-
),
1210-
file_names,
1202+
executor_ctx: multiprocessing.pool.Pool | AbstractContextManager[None] = (
1203+
multiprocessing.pool.Pool(jobs if jobs > 0 else multiprocessing.cpu_count())
12111204
)
12121205
else:
1213-
# https://github.com/python/typeshed/pull/2814
1214-
attempt_iterator = (
1215-
sort_imports( # type: ignore
1216-
file_name,
1217-
config=config,
1218-
check=check,
1219-
ask_to_apply=ask_to_apply,
1220-
show_diff=show_diff,
1221-
write_to_stdout=write_to_stdout,
1222-
extension=ext_format,
1223-
config_trie=config_trie,
1206+
executor_ctx = nullcontext()
1207+
1208+
with executor_ctx as executor:
1209+
if executor is not None:
1210+
attempt_iterator = executor.imap(
1211+
functools.partial(
1212+
sort_imports,
1213+
config=config,
1214+
check=check,
1215+
ask_to_apply=ask_to_apply,
1216+
show_diff=show_diff,
1217+
write_to_stdout=write_to_stdout,
1218+
extension=ext_format,
1219+
config_trie=config_trie,
1220+
),
1221+
file_names,
12241222
)
1225-
for file_name in file_names
1226-
)
1227-
1228-
# If any files passed in are missing considered as error, should be removed
1229-
is_no_attempt = True
1230-
any_encoding_valid = False
1231-
for sort_attempt in attempt_iterator:
1232-
if not sort_attempt:
1233-
continue # pragma: no cover - shouldn't happen, satisfies type constraint
1234-
incorrectly_sorted = sort_attempt.incorrectly_sorted
1235-
if arguments.get("check", False) and incorrectly_sorted:
1236-
wrong_sorted_files = True
1237-
if sort_attempt.skipped:
1238-
num_skipped += (
1239-
1 # pragma: no cover - shouldn't happen, due to skip in iter_source_code
1223+
else:
1224+
# https://github.com/python/typeshed/pull/2814
1225+
attempt_iterator = (
1226+
sort_imports( # type: ignore
1227+
file_name,
1228+
config=config,
1229+
check=check,
1230+
ask_to_apply=ask_to_apply,
1231+
show_diff=show_diff,
1232+
write_to_stdout=write_to_stdout,
1233+
extension=ext_format,
1234+
config_trie=config_trie,
1235+
)
1236+
for file_name in file_names
12401237
)
12411238

1242-
if not sort_attempt.supported_encoding:
1243-
num_invalid_encoding += 1
1244-
else:
1245-
any_encoding_valid = True
1239+
# If any files passed in are missing considered as error, should be removed
1240+
is_no_attempt = True
1241+
any_encoding_valid = False
1242+
for sort_attempt in attempt_iterator:
1243+
if not sort_attempt:
1244+
continue # pragma: no cover - shouldn't happen, satisfies type constraint
1245+
incorrectly_sorted = sort_attempt.incorrectly_sorted
1246+
if arguments.get("check", False) and incorrectly_sorted:
1247+
wrong_sorted_files = True
1248+
if sort_attempt.skipped:
1249+
num_skipped += (
1250+
1 # pragma: no cover - shouldn't happen, due to skip in iter_source_code
1251+
)
1252+
1253+
if not sort_attempt.supported_encoding:
1254+
num_invalid_encoding += 1
1255+
else:
1256+
any_encoding_valid = True
12461257

1247-
is_no_attempt = False
1258+
is_no_attempt = False
12481259

12491260
num_skipped += len(skipped)
12501261
if num_skipped and not config.quiet:

0 commit comments

Comments
 (0)