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

Allow users to provide an explicit process count #78

Merged
merged 7 commits into from
Oct 11, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 20 additions & 2 deletions sphinxlint/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,14 @@ def __call__(self, parser, namespace, values, option_string=None):
) from None
setattr(namespace, self.dest, sort_fields)

class StoreNumJobsAction(argparse.Action):
def __call__(self, parser, namespace, values, option_string=None):
setattr(namespace, self.dest, StoreNumJobsAction._job_count(values))
def _job_count(values):
if values == "auto":
return os.cpu_count()
return max(int(values), 1)

parser.add_argument(
"-v",
"--verbose",
Expand Down Expand Up @@ -109,6 +117,16 @@ def __call__(self, parser, namespace, values, option_string=None):
help="comma-separated list of fields used to sort errors by. Available "
f"fields are: {SortField.as_supported_options()}",
)
parser.add_argument(
"-j",
"--jobs",
metavar="N",
action=StoreNumJobsAction,
help="Run in parallle with N processes, defaults to \"auto\". "
"Special value \"auto\" will set N to cpu-count. "
"Values <= 1 are all considered 1.",
default=StoreNumJobsAction._job_count("auto")
)
parser.add_argument(
"-V", "--version", action="version", version=f"%(prog)s {__version__}"
)
Expand Down Expand Up @@ -209,10 +227,10 @@ def main(argv=None):
for path in chain.from_iterable(walk(path, args.ignore) for path in args.paths)
]

if len(todo) < 8:
if args.jobs == 1 or len(todo) < 8:
count = print_errors(sort_errors(starmap(check_file, todo), args.sort_by))
else:
with multiprocessing.Pool() as pool:
with multiprocessing.Pool(processes=args.jobs) as pool:
count = print_errors(
sort_errors(pool.imap_unordered(_check_file, todo), args.sort_by)
)
Expand Down