Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add --workers CLI parameter (fixes #2513) #2514

Merged
merged 2 commits into from Sep 29, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
17 changes: 15 additions & 2 deletions src/black/__init__.py
Expand Up @@ -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()]",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
type=int,
help="Number of parallel workers [default: os.cpu_count()]",
default=os.cpu_count(),
type=int,
show_default=True,
help="Number of parallel workers",

Maybe we could just show the cpu_count in the help output ...

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could be confusing from one machine to another and on our documentation. Probably not that big of a deal though, and it is convenient to see the value right away 🤔

)
@click.option(
"-q",
"--quiet",
Expand Down Expand Up @@ -383,6 +389,7 @@ def main(
extend_exclude: Optional[Pattern],
force_exclude: Optional[Pattern],
stdin_filename: Optional[str],
workers: Optional[int],
FHTMitchell marked this conversation as resolved.
Show resolved Hide resolved
src: Tuple[str, ...],
config: Optional[str],
) -> None:
Expand Down Expand Up @@ -468,6 +475,7 @@ def main(
write_back=write_back,
mode=mode,
report=report,
workers=workers,
)

if verbose or not quiet:
Expand Down Expand Up @@ -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],
FHTMitchell marked this conversation as resolved.
Show resolved Hide resolved
) -> 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()
FHTMitchell marked this conversation as resolved.
Show resolved Hide resolved
if sys.platform == "win32":
# Work around https://bugs.python.org/issue26903
worker_count = min(worker_count, 60)
Expand Down