Patchwork [06,of,11] worker: count the number of CPUs

login
register
mail settings
Submitter Bryan O'Sullivan
Date Feb. 9, 2013, 2:06 p.m.
Message ID <06aa98485023be2c4275.1360418806@australite.local>
Download mbox | patch
Permalink /patch/871/
State Accepted
Commit fed06dd07665515de823c912bc94b8de087af314
Headers show

Comments

Bryan O'Sullivan - Feb. 9, 2013, 2:06 p.m.
# HG changeset patch
# User Bryan O'Sullivan <bryano@fb.com>
# Date 1360418465 0
# Node ID 06aa98485023be2c427552053398e8217db7b4d6
# Parent  849757bef45281eefa0e7425865b00ddf4159f5d
worker: count the number of CPUs

This works on the major platforms, and falls back to a safe guess of
1 elsewhere.

Patch

diff --git a/mercurial/worker.py b/mercurial/worker.py
new file mode 100644
--- /dev/null
+++ b/mercurial/worker.py
@@ -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