Skip to content

Commit

Permalink
Add --workers CLI parameter (fixes #2513) (#2514)
Browse files Browse the repository at this point in the history
Fixes #2513
  • Loading branch information
FHTMitchell committed Sep 29, 2021
1 parent 09915f4 commit 0fd353f
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGES.md
Expand Up @@ -2,6 +2,10 @@

## Unreleased

### _Black_

- Add new `--workers` parameter (#2514)

### _Blackd_

- Remove dependency on aiohttp-cors (#2500)
Expand Down
21 changes: 19 additions & 2 deletions src/black/__init__.py
Expand Up @@ -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]
Expand Down Expand Up @@ -318,6 +320,14 @@ def validate_regex(
"editors that rely on using stdin."
),
)
@click.option(
"-W",
"--workers",
type=click.IntRange(min=1),
default=DEFAULT_WORKERS,
show_default=True,
help="Number of parallel workers",
)
@click.option(
"-q",
"--quiet",
Expand Down Expand Up @@ -383,6 +393,7 @@ def main(
extend_exclude: Optional[Pattern],
force_exclude: Optional[Pattern],
stdin_filename: Optional[str],
workers: int,
src: Tuple[str, ...],
config: Optional[str],
) -> None:
Expand Down Expand Up @@ -468,6 +479,7 @@ def main(
write_back=write_back,
mode=mode,
report=report,
workers=workers,
)

if verbose or not quiet:
Expand Down Expand Up @@ -644,12 +656,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 DEFAULT_WORKERS
if sys.platform == "win32":
# Work around https://bugs.python.org/issue26903
worker_count = min(worker_count, 60)
Expand Down

0 comments on commit 0fd353f

Please sign in to comment.