Skip to content

Commit 9c47b6e

Browse files
authored
Avoid using an extra process when running with only one worker (#4734)
1 parent b3ae57b commit 9c47b6e

File tree

2 files changed

+17
-8
lines changed

2 files changed

+17
-8
lines changed

CHANGES.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@
5353

5454
<!-- Changes that improve Black's performance. -->
5555

56+
- Avoid using an extra process when running with only one worker (#4734)
57+
5658
### Output
5759

5860
<!-- Changes to Black's terminal output and error messages -->

src/black/concurrency.py

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
NOTE: this module is only imported if we need to format several files at once.
55
"""
66

7+
from __future__ import annotations
8+
79
import asyncio
810
import logging
911
import os
@@ -80,20 +82,25 @@ def reformat_many(
8082
"""Reformat multiple files using a ProcessPoolExecutor."""
8183
maybe_install_uvloop()
8284

83-
executor: Executor
8485
if workers is None:
8586
workers = int(os.environ.get("BLACK_NUM_WORKERS", 0))
8687
workers = workers or os.cpu_count() or 1
8788
if sys.platform == "win32":
8889
# Work around https://bugs.python.org/issue26903
8990
workers = min(workers, 60)
90-
try:
91-
executor = ProcessPoolExecutor(max_workers=workers)
92-
except (ImportError, NotImplementedError, OSError):
93-
# we arrive here if the underlying system does not support multi-processing
94-
# like in AWS Lambda or Termux, in which case we gracefully fallback to
95-
# a ThreadPoolExecutor with just a single worker (more workers would not do us
96-
# any good due to the Global Interpreter Lock)
91+
92+
executor: Executor | None = None
93+
if workers > 1:
94+
try:
95+
executor = ProcessPoolExecutor(max_workers=workers)
96+
except (ImportError, NotImplementedError, OSError):
97+
# we arrive here if the underlying system does not support multi-processing
98+
# like in AWS Lambda or Termux, in which case we gracefully fallback to
99+
# a ThreadPoolExecutor with just a single worker (more workers would not do
100+
# us any good due to the Global Interpreter Lock)
101+
pass
102+
103+
if executor is None:
97104
executor = ThreadPoolExecutor(max_workers=1)
98105

99106
loop = asyncio.new_event_loop()

0 commit comments

Comments
 (0)