Skip to content

Commit

Permalink
Autodetect number of jobs with --jobs auto (#605)
Browse files Browse the repository at this point in the history
This adds the possibility to autodetect the number of jobs based on the number of available CPU cores on the host system using `pytype --jobs auto`. This allows for a usage similar to [`pytest-xdist`](https://github.com/pytest-dev/pytest-xdist#speed-up-test-runs-by-sending-tests-to-multiple-cpus) which can be useful when using `pytype` in scripts that might run on different systems.

Resolves #605

PiperOrigin-RevId: 317924811
  • Loading branch information
lgeiger authored and rchen152 committed Jun 23, 2020
1 parent 28a2427 commit 274cb27
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
3 changes: 2 additions & 1 deletion pytype/tools/analyze_project/config.py
Expand Up @@ -48,7 +48,8 @@
'Keep going past errors to analyze as many files as possible.'),
'jobs': Item(
1, '4', None,
'Run N jobs in parallel.'),
"Run N jobs in parallel. When 'auto' is used, this will be equivalent "
'to the number of CPUs on the host system.'),
'output': Item(
'.pytype', '.pytype', None, 'All pytype output goes here.'),
'pythonpath': Item(
Expand Down
22 changes: 21 additions & 1 deletion pytype/tools/analyze_project/parse_args.py
@@ -1,6 +1,7 @@
"""Argument parsing for analyze_project."""

import argparse
import os

from pytype import config as pytype_config
from pytype.tools import arg_parser
Expand All @@ -10,6 +11,24 @@
_ARG_PREFIX = '--'


def _auto_detect_cpus():
try:
return len(os.sched_getaffinity(0))
except AttributeError:
return os.cpu_count()


def parse_jobs(s):
"""Parse the --jobs option."""
if s == 'auto':
n = _auto_detect_cpus()
return n if n else 1
elif s is not None:
return int(s)
else:
return None


class Parser(arg_parser.Parser):
"""Subclasses Parser to add a config file processor."""

Expand Down Expand Up @@ -93,7 +112,8 @@ def make_parser():
(('-x', '--exclude'), {'nargs': '*', 'action': 'flatten'}),
(('inputs',), {'metavar': 'input', 'nargs': '*', 'action': 'flatten'}),
(('-k', '--keep-going'), {'action': 'store_true', 'type': None}),
(('-j', '--jobs'), {'action': 'store', 'type': int, 'metavar': 'N'}),
(('-j', '--jobs'), {'action': 'store', 'type': parse_jobs,
'metavar': 'N'}),
(('-P', '--pythonpath'),),
(('-V', '--python-version'),)
]:
Expand Down

0 comments on commit 274cb27

Please sign in to comment.