Mercurial > hg-stable
changeset 18635:fed06dd07665
worker: count the number of CPUs
This works on the major platforms, and falls back to a safe guess of
1 elsewhere.
author | Bryan O'Sullivan <bryano@fb.com> |
---|---|
date | Sat, 09 Feb 2013 15:22:12 -0800 |
parents | 4b5d37ca3c11 |
children | dcb27c153a40 |
files | mercurial/worker.py |
diffstat | 1 files changed, 29 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mercurial/worker.py Sat Feb 09 15:22:12 2013 -0800 @@ -0,0 +1,29 @@ +# worker.py - master-slave parallelism support +# +# Copyright 2013 Facebook, Inc. +# +# This software may be used and distributed according to the terms of the +# GNU General Public License version 2 or any later version. + +import os + +def countcpus(): + '''try to count the number of CPUs on the system''' + + # posix + try: + n = int(os.sysconf('SC_NPROCESSORS_ONLN')) + if n > 0: + return n + except (AttributeError, ValueError): + pass + + # windows + try: + n = int(os.environ['NUMBER_OF_PROCESSORS']) + if n > 0: + return n + except (KeyError, ValueError): + pass + + return 1