Skip to content

Commit

Permalink
Merge pull request #78 from rtobar/explicit-process-count
Browse files Browse the repository at this point in the history
  • Loading branch information
hugovk authored Oct 11, 2023
2 parents 9c86368 + 9e06ae3 commit 1e8c3f9
Showing 1 changed file with 22 additions and 2 deletions.
24 changes: 22 additions & 2 deletions sphinxlint/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,16 @@ 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, self._job_count(values))

@staticmethod
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 +119,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 parallel with N processes. Defaults to 'auto', "
"which sets N to the number of logical CPUs."
"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 +229,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

0 comments on commit 1e8c3f9

Please sign in to comment.