Skip to content

Autodetect number of jobs with --jobs auto#605

Closed
lgeiger wants to merge 2 commits into
google:masterfrom
lgeiger:autodetect-jobs
Closed

Autodetect number of jobs with --jobs auto#605
lgeiger wants to merge 2 commits into
google:masterfrom
lgeiger:autodetect-jobs

Conversation

@lgeiger

@lgeiger lgeiger commented Jun 20, 2020

Copy link
Copy Markdown
Contributor

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 which can be useful when using pytype in scripts that might run on different systems.


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

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This return the number of CPUs the current process is restricted to on Linux.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

What's wrong with os.cpu_count()?

@lgeiger lgeiger Jun 20, 2020

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

On Linux len(os.sched_getaffinity(0)) returns the number of CPUs available to the current process, where as os.cpu_count() will return the total number of CPUs on the host system which might be more than the current process can use.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

answering my question - it's the number of available cores (not clear whether this includes hyperthreading cores).
More details: https://stackoverflow.com/questions/1006289/how-to-find-out-the-number-of-cpus-using-python/14840102

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

not clear whether this includes hyperthreading cores

As far as I know this includes hyperthreading cores. On my dual-core macbook --jobs auto will result in 4 parallel jobs.

@rchen152 rchen152 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks! Just a few nits related to the Google style guide.

Comment thread pytype/tools/analyze_project/parse_args.py
Comment thread pytype/tools/analyze_project/parse_args.py Outdated
Comment thread pytype/tools/analyze_project/parse_args.py
@lgeiger

lgeiger commented Jun 22, 2020

Copy link
Copy Markdown
Contributor Author

Just a few nits related to the Google style guide.

Fixed in 4e48291

@lgeiger lgeiger requested a review from rchen152 June 22, 2020 22:28
@rchen152 rchen152 closed this in 274cb27 Jun 23, 2020
@lgeiger lgeiger deleted the autodetect-jobs branch June 23, 2020 22:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants