Rev 5683: (jelmer) Use multiprocessing.cpu_count() to retrieve the number of CPUs on in file:///home/pqm/archives/thelove/bzr/%2Btrunk/

Canonical.com Patch Queue Manager pqm at pqm.ubuntu.com
Wed Feb 23 17:41:32 UTC 2011


At file:///home/pqm/archives/thelove/bzr/%2Btrunk/

------------------------------------------------------------
revno: 5683 [merge]
revision-id: pqm at pqm.ubuntu.com-20110223174127-wwpmy437d2g2e1ny
parent: pqm at pqm.ubuntu.com-20110223163601-v9ii6owx2k3vx213
parent: jelmer at samba.org-20110223135040-0v0kakb785d6sz8z
committer: Canonical.com Patch Queue Manager <pqm at pqm.ubuntu.com>
branch nick: +trunk
timestamp: Wed 2011-02-23 17:41:27 +0000
message:
  (jelmer) Use multiprocessing.cpu_count() to retrieve the number of CPUs on
   Python >= 2.6. (Jelmer Vernooij)
modified:
  bzrlib/osutils.py              osutils.py-20050309040759-eeaff12fbf77ac86
  doc/en/release-notes/bzr-2.4.txt bzr2.4.txt-20110114053217-k7ym9jfz243fddjm-1
=== modified file 'bzrlib/osutils.py'
--- a/bzrlib/osutils.py	2011-01-16 01:12:01 +0000
+++ b/bzrlib/osutils.py	2011-02-22 22:35:08 +0000
@@ -2243,20 +2243,17 @@
             termios.tcsetattr(fd, termios.TCSADRAIN, settings)
         return ch
 
-
 if sys.platform == 'linux2':
     def _local_concurrency():
-        concurrency = None
-        prefix = 'processor'
-        for line in file('/proc/cpuinfo', 'rb'):
-            if line.startswith(prefix):
-                concurrency = int(line[line.find(':')+1:]) + 1
-        return concurrency
+        try:
+            return os.sysconf('SC_NPROCESSORS_ONLN')
+        except (ValueError, OSError, AttributeError):
+            return None
 elif sys.platform == 'darwin':
     def _local_concurrency():
         return subprocess.Popen(['sysctl', '-n', 'hw.availcpu'],
                                 stdout=subprocess.PIPE).communicate()[0]
-elif sys.platform[0:7] == 'freebsd':
+elif "bsd" in sys.platform:
     def _local_concurrency():
         return subprocess.Popen(['sysctl', '-n', 'hw.ncpu'],
                                 stdout=subprocess.PIPE).communicate()[0]
@@ -2290,9 +2287,15 @@
     concurrency = os.environ.get('BZR_CONCURRENCY', None)
     if concurrency is None:
         try:
-            concurrency = _local_concurrency()
-        except (OSError, IOError):
-            pass
+            import multiprocessing
+        except ImportError:
+            # multiprocessing is only available on Python >= 2.6
+            try:
+                concurrency = _local_concurrency()
+            except (OSError, IOError):
+                pass
+        else:
+            concurrency = multiprocessing.cpu_count()
     try:
         concurrency = int(concurrency)
     except (TypeError, ValueError):

=== modified file 'doc/en/release-notes/bzr-2.4.txt'
--- a/doc/en/release-notes/bzr-2.4.txt	2011-02-23 16:36:01 +0000
+++ b/doc/en/release-notes/bzr-2.4.txt	2011-02-23 17:41:27 +0000
@@ -103,6 +103,9 @@
 * Fix ``bzr lp-mirror`` to work on command line branch URLs and branches
   without an explicit public location. (Max Bowsher)
 
+* On Python 2.6 and higher, use multiprocessing.cpu_count() to retrieve the
+  number of available processors. (Jelmer Vernooij, #693140)
+
 Documentation
 *************
 




More information about the bazaar-commits mailing list