worker: use multiprocessing to find cpu count
authorGregory Szorc <gregory.szorc@gmail.com>
Mon, 25 May 2015 13:10:38 -0700
changeset 26063 d29859cfcfc2
parent 26062 7154a4a08b96
child 26064 1b1ab6ff58c4
worker: use multiprocessing to find cpu count The multiprocessing package was added in Python 2.6. The implementation of worker.countcpus() is very similar to multiprocessing.cpu_count(). Ditch our one-off code. multiprocessing does result in a number of imports. However, the lazy importer ensures that we don't import anything until cpu_count() is called. Furthermore, if we are doing something with multiple cores, chances are the time of that operation will dwarf the import time, so module bloat isn't a concern here.
mercurial/worker.py
--- a/mercurial/worker.py	Mon Aug 24 10:09:00 2015 -0400
+++ b/mercurial/worker.py	Mon May 25 13:10:38 2015 -0700
@@ -8,6 +8,7 @@
 from __future__ import absolute_import
 
 import errno
+import multiprocessing
 import os
 import signal
 import sys
@@ -18,24 +19,10 @@
 
 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
+        return multiprocessing.cpu_count()
+    except NotImplementedError:
+        return 1
 
 def _numworkers(ui):
     s = ui.config('worker', 'numcpus')