From c5f1283527055fb5425f5b56130f00e5eb12c8e1 Mon Sep 17 00:00:00 2001 From: hauntsaninja Date: Sun, 17 Aug 2025 16:20:16 -0700 Subject: [PATCH 1/5] Avoid using an extra process when running with only one worker --- CHANGES.md | 1 + src/black/concurrency.py | 21 +++++++++++++-------- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index da991a69eee..92e6ad48189 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -52,6 +52,7 @@ ### Performance +- Avoid using an extra process when running with only one worker ### Output diff --git a/src/black/concurrency.py b/src/black/concurrency.py index 4b3cf48d901..3b050748005 100644 --- a/src/black/concurrency.py +++ b/src/black/concurrency.py @@ -80,20 +80,25 @@ def reformat_many( """Reformat multiple files using a ProcessPoolExecutor.""" maybe_install_uvloop() - executor: Executor if workers is None: workers = int(os.environ.get("BLACK_NUM_WORKERS", 0)) workers = workers or os.cpu_count() or 1 if sys.platform == "win32": # Work around https://bugs.python.org/issue26903 workers = min(workers, 60) - try: - executor = ProcessPoolExecutor(max_workers=workers) - except (ImportError, NotImplementedError, OSError): - # we arrive here if the underlying system does not support multi-processing - # like in AWS Lambda or Termux, in which case we gracefully fallback to - # a ThreadPoolExecutor with just a single worker (more workers would not do us - # any good due to the Global Interpreter Lock) + + executor: Executor | None = None + if workers > 1: + try: + executor = ProcessPoolExecutor(max_workers=workers) + except (ImportError, NotImplementedError, OSError): + # we arrive here if the underlying system does not support multi-processing + # like in AWS Lambda or Termux, in which case we gracefully fallback to + # a ThreadPoolExecutor with just a single worker (more workers would not do us + # any good due to the Global Interpreter Lock) + pass + + if executor is None: executor = ThreadPoolExecutor(max_workers=1) loop = asyncio.new_event_loop() From 674a55d87db21953c29ae848e8a27820c0874cf4 Mon Sep 17 00:00:00 2001 From: hauntsaninja Date: Sun, 17 Aug 2025 16:20:57 -0700 Subject: [PATCH 2/5] changelog --- CHANGES.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGES.md b/CHANGES.md index 92e6ad48189..97750ee3c02 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -52,7 +52,7 @@ ### Performance -- Avoid using an extra process when running with only one worker +- Avoid using an extra process when running with only one worker (#4734) ### Output From b9f7b219bedad07f91a046b02d8d78887600df68 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 17 Aug 2025 23:21:35 +0000 Subject: [PATCH 3/5] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- CHANGES.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGES.md b/CHANGES.md index 97750ee3c02..e87b61dbaeb 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -52,6 +52,7 @@ ### Performance + - Avoid using an extra process when running with only one worker (#4734) ### Output From 08817db4f3b3c8f43c89056cadcd05a9a3e01353 Mon Sep 17 00:00:00 2001 From: hauntsaninja Date: Sun, 17 Aug 2025 16:51:32 -0700 Subject: [PATCH 4/5] . --- src/black/concurrency.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/black/concurrency.py b/src/black/concurrency.py index 3b050748005..afc54bb6d9b 100644 --- a/src/black/concurrency.py +++ b/src/black/concurrency.py @@ -4,6 +4,8 @@ NOTE: this module is only imported if we need to format several files at once. """ +from __future__ import annotations + import asyncio import logging import os From fc409d23816186499016a191778ced71228f94d6 Mon Sep 17 00:00:00 2001 From: hauntsaninja Date: Sun, 17 Aug 2025 16:59:57 -0700 Subject: [PATCH 5/5] . --- src/black/concurrency.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/black/concurrency.py b/src/black/concurrency.py index afc54bb6d9b..f6a2b8a93be 100644 --- a/src/black/concurrency.py +++ b/src/black/concurrency.py @@ -96,8 +96,8 @@ def reformat_many( except (ImportError, NotImplementedError, OSError): # we arrive here if the underlying system does not support multi-processing # like in AWS Lambda or Termux, in which case we gracefully fallback to - # a ThreadPoolExecutor with just a single worker (more workers would not do us - # any good due to the Global Interpreter Lock) + # a ThreadPoolExecutor with just a single worker (more workers would not do + # us any good due to the Global Interpreter Lock) pass if executor is None: