Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1713)

Unified Diff: gclient_utils.py

Issue 819133003: Add cross-platform resilience to gclient CPU count probe. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/tools/depot_tools/
Patch Set: Created 5 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: gclient_utils.py
===================================================================
--- gclient_utils.py (revision 293519)
+++ gclient_utils.py (working copy)
@@ -1141,16 +1141,33 @@
def NumLocalCpus():
"""Returns the number of processors.
- Python on OSX 10.6 raises a NotImplementedError exception.
+ multiprocessing.cpu_count() is permitted to raise NotImplementedError, and
+ is known to do this on some Windows systems and OSX 10.6. If we can't get the
+ CPU count, we will fall back to '1'.
"""
+ # Surround the entire thing in try/except; no failure here should stop gclient
+ # from working.
try:
- import multiprocessing
- return multiprocessing.cpu_count()
- except: # pylint: disable=W0702
- # Mac OS 10.6 only
- # pylint: disable=E1101
- return int(os.sysconf('SC_NPROCESSORS_ONLN'))
+ # Use multiprocessing to get CPU count. This may raise
+ # NotImplementedError.
+ try:
+ import multiprocessing
+ return multiprocessing.cpu_count()
+ except NotImplementedError: # pylint: disable=W0702
+ # (UNIX) Query 'os.sysconf'.
+ # pylint: disable=E1101
+ if hasattr(os, 'sysconf') and 'SC_NPROCESSORS_ONLN' in os.sysconf_names:
+ return int(os.sysconf('SC_NPROCESSORS_ONLN'))
+ # (Windows) Query 'NUMBER_OF_PROCESSORS' environment variable.
+ if 'NUMBER_OF_PROCESSORS' in os.environ:
+ return int(os.environ['NUMBER_OF_PROCESSORS'])
+ except Exception as e:
+ logging.exception("Exception raised while probing CPU count: %s", e)
+
+ logging.debug('Failed to get CPU count. Defaulting to 1.')
+ return 1
+
def DefaultDeltaBaseCacheLimit():
"""Return a reasonable default for the git config core.deltaBaseCacheLimit.
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698