worker: restore old countcpus code (issue4869)
authorGregory Szorc <gregory.szorc@gmail.com>
Thu, 08 Oct 2015 10:57:03 -0700
changeset 26568 c0501c26b05c
parent 26567 f18646cf0e93
child 26569 2aeeef1dc9a5
worker: restore old countcpus code (issue4869) This is a backout of d29859cfcfc2. The stdlib implementation of multiprocessing.cpu_count() attempts to invoke a process on BSD and Darwin platforms (at least on 2.7). Under certain conditions (such as cwd being removed) this could raise. Our old code was silently catching the exception. The old code was more robust, so restore it.
mercurial/worker.py
--- a/mercurial/worker.py	Wed Oct 07 21:22:16 2015 -0700
+++ b/mercurial/worker.py	Thu Oct 08 10:57:03 2015 -0700
@@ -8,7 +8,6 @@
 from __future__ import absolute_import
 
 import errno
-import multiprocessing
 import os
 import signal
 import sys
@@ -19,10 +18,24 @@
 
 def countcpus():
     '''try to count the number of CPUs on the system'''
+
+    # posix
     try:
-        return multiprocessing.cpu_count()
-    except NotImplementedError:
-        return 1
+        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
 
 def _numworkers(ui):
     s = ui.config('worker', 'numcpus')