Skip to content

Commit 1e8c3f9

Browse files
authored
Merge pull request #78 from rtobar/explicit-process-count
2 parents 9c86368 + 9e06ae3 commit 1e8c3f9

File tree

1 file changed

+22
-2
lines changed

1 file changed

+22
-2
lines changed

sphinxlint/__main__.py

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,16 @@ def __call__(self, parser, namespace, values, option_string=None):
5858
) from None
5959
setattr(namespace, self.dest, sort_fields)
6060

61+
class StoreNumJobsAction(argparse.Action):
62+
def __call__(self, parser, namespace, values, option_string=None):
63+
setattr(namespace, self.dest, self._job_count(values))
64+
65+
@staticmethod
66+
def job_count(values):
67+
if values == "auto":
68+
return os.cpu_count()
69+
return max(int(values), 1)
70+
6171
parser.add_argument(
6272
"-v",
6373
"--verbose",
@@ -109,6 +119,16 @@ def __call__(self, parser, namespace, values, option_string=None):
109119
help="comma-separated list of fields used to sort errors by. Available "
110120
f"fields are: {SortField.as_supported_options()}",
111121
)
122+
parser.add_argument(
123+
"-j",
124+
"--jobs",
125+
metavar="N",
126+
action=StoreNumJobsAction,
127+
help="Run in parallel with N processes. Defaults to 'auto', "
128+
"which sets N to the number of logical CPUs."
129+
"Values <= 1 are all considered 1.",
130+
default=StoreNumJobsAction.job_count("auto")
131+
)
112132
parser.add_argument(
113133
"-V", "--version", action="version", version=f"%(prog)s {__version__}"
114134
)
@@ -209,10 +229,10 @@ def main(argv=None):
209229
for path in chain.from_iterable(walk(path, args.ignore) for path in args.paths)
210230
]
211231

212-
if len(todo) < 8:
232+
if args.jobs == 1 or len(todo) < 8:
213233
count = print_errors(sort_errors(starmap(check_file, todo), args.sort_by))
214234
else:
215-
with multiprocessing.Pool() as pool:
235+
with multiprocessing.Pool(processes=args.jobs) as pool:
216236
count = print_errors(
217237
sort_errors(pool.imap_unordered(_check_file, todo), args.sort_by)
218238
)

0 commit comments

Comments
 (0)