[PATCH] worker: use multiprocessing to find cpu count

Gregory Szorc gregory.szorc at gmail.com
Sat Aug 22 16:52:07 UTC 2015


# HG changeset patch
# User Gregory Szorc <gregory.szorc at gmail.com>
# Date 1432584638 25200
#      Mon May 25 13:10:38 2015 -0700
# Node ID a3f28ff761510272136887b739fb3a81d0d5a9af
# Parent  d9d3d49c4cf77049d12920980e91bf8e4a4ecda2
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.

diff --git a/mercurial/worker.py b/mercurial/worker.py
--- a/mercurial/worker.py
+++ b/mercurial/worker.py
@@ -7,8 +7,9 @@
 
 from __future__ import absolute_import
 
 import errno
+import multiprocessing
 import os
 import signal
 import sys
 import threading
@@ -17,26 +18,12 @@ from .i18n import _
 from . import util
 
 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')
     if s:


More information about the Mercurial-devel mailing list