From 23b1f7ac7d842ccad81703a1485fee8b02522049 Mon Sep 17 00:00:00 2001 From: Fergus Mitchell Date: Mon, 27 Sep 2021 19:26:36 +0100 Subject: [PATCH 1/2] Add --workers CLI parameter (fixes #2513) --- CHANGES.md | 4 ++++ src/black/__init__.py | 17 +++++++++++++++-- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index 1ff2cea84ca..4b30a2df1b7 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -2,6 +2,10 @@ ## Unreleased +### _Black_ + +- Add new `--workers` parameter (#2514) + ### _Blackd_ - Remove dependency on aiohttp-cors (#2500) diff --git a/src/black/__init__.py b/src/black/__init__.py index 7fed1355f47..9befefbc37a 100644 --- a/src/black/__init__.py +++ b/src/black/__init__.py @@ -318,6 +318,12 @@ def validate_regex( "editors that rely on using stdin." ), ) +@click.option( + "-W", + "--workers", + type=int, + help="Number of parallel workers [default: os.cpu_count()]", +) @click.option( "-q", "--quiet", @@ -383,6 +389,7 @@ def main( extend_exclude: Optional[Pattern], force_exclude: Optional[Pattern], stdin_filename: Optional[str], + workers: Optional[int], src: Tuple[str, ...], config: Optional[str], ) -> None: @@ -468,6 +475,7 @@ def main( write_back=write_back, mode=mode, report=report, + workers=workers, ) if verbose or not quiet: @@ -644,12 +652,17 @@ def reformat_one( def reformat_many( - sources: Set[Path], fast: bool, write_back: WriteBack, mode: Mode, report: "Report" + sources: Set[Path], + fast: bool, + write_back: WriteBack, + mode: Mode, + report: "Report", + workers: Optional[int], ) -> None: """Reformat multiple files using a ProcessPoolExecutor.""" executor: Executor loop = asyncio.get_event_loop() - worker_count = os.cpu_count() + worker_count = workers if workers is not None else os.cpu_count() if sys.platform == "win32": # Work around https://bugs.python.org/issue26903 worker_count = min(worker_count, 60) From 86305a6908f2aa9637dbe344ed69cf3ac2d12612 Mon Sep 17 00:00:00 2001 From: Fergus Mitchell Date: Wed, 29 Sep 2021 10:57:44 +0100 Subject: [PATCH 2/2] Set the workers default to os.cpu_count (rather than None) --- src/black/__init__.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/black/__init__.py b/src/black/__init__.py index 9befefbc37a..83a39234d38 100644 --- a/src/black/__init__.py +++ b/src/black/__init__.py @@ -95,6 +95,8 @@ def from_configuration( # Legacy name, left for integrations. FileMode = Mode +DEFAULT_WORKERS = os.cpu_count() + def read_pyproject_toml( ctx: click.Context, param: click.Parameter, value: Optional[str] @@ -321,8 +323,10 @@ def validate_regex( @click.option( "-W", "--workers", - type=int, - help="Number of parallel workers [default: os.cpu_count()]", + type=click.IntRange(min=1), + default=DEFAULT_WORKERS, + show_default=True, + help="Number of parallel workers", ) @click.option( "-q", @@ -389,7 +393,7 @@ def main( extend_exclude: Optional[Pattern], force_exclude: Optional[Pattern], stdin_filename: Optional[str], - workers: Optional[int], + workers: int, src: Tuple[str, ...], config: Optional[str], ) -> None: @@ -662,7 +666,7 @@ def reformat_many( """Reformat multiple files using a ProcessPoolExecutor.""" executor: Executor loop = asyncio.get_event_loop() - worker_count = workers if workers is not None else os.cpu_count() + worker_count = workers if workers is not None else DEFAULT_WORKERS if sys.platform == "win32": # Work around https://bugs.python.org/issue26903 worker_count = min(worker_count, 60)