[issue17914] add os.cpu_count()

Charles-François Natali report at bugs.python.org
Mon May 20 14:57:01 CEST 2013


Charles-François Natali added the comment:

Alright, committed.
Yogesh, thanks for the patch!

I'm attaching a patch to replace several occurrences of
multiprocessing.cpu_count() by os.cpu_count() in the stdlib/test
suite.

----------
Added file: http://bugs.python.org/file30319/use_cpu_count.diff

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue17914>
_______________________________________
-------------- next part --------------
diff -r 5e0c56557390 Doc/library/multiprocessing.rst
--- a/Doc/library/multiprocessing.rst	Mon May 20 14:40:46 2013 +0200
+++ b/Doc/library/multiprocessing.rst	Mon May 20 14:52:18 2013 +0200
@@ -1646,9 +1646,9 @@
    callbacks and has a parallel map implementation.
 
    *processes* is the number of worker processes to use.  If *processes* is
-   ``None`` then the number returned by :func:`cpu_count` is used.  If
-   *initializer* is not ``None`` then each worker process will call
-   ``initializer(*initargs)`` when it starts.
+   ``None`` then the number returned by :func:`os.cpu_count` is used, with a
+   fallback value of 1.  If *initializer* is not ``None`` then each worker
+   process will call ``initializer(*initargs)`` when it starts.
 
    .. versionadded:: 3.2
       *maxtasksperchild* is the number of tasks a worker process can complete
diff -r 5e0c56557390 Lib/concurrent/futures/process.py
--- a/Lib/concurrent/futures/process.py	Mon May 20 14:40:46 2013 +0200
+++ b/Lib/concurrent/futures/process.py	Mon May 20 14:52:18 2013 +0200
@@ -331,7 +331,7 @@
         _check_system_limits()
 
         if max_workers is None:
-            self._max_workers = multiprocessing.cpu_count()
+            self._max_workers = os.cpu_count() or 1
         else:
             self._max_workers = max_workers
 
diff -r 5e0c56557390 Lib/multiprocessing/pool.py
--- a/Lib/multiprocessing/pool.py	Mon May 20 14:40:46 2013 +0200
+++ b/Lib/multiprocessing/pool.py	Mon May 20 14:52:18 2013 +0200
@@ -17,10 +17,11 @@
 import queue
 import itertools
 import collections
+import os
 import time
 import traceback
 
-from multiprocessing import Process, cpu_count, TimeoutError
+from multiprocessing import Process, TimeoutError
 from multiprocessing.util import Finalize, debug
 
 #
@@ -147,10 +148,7 @@
         self._initargs = initargs
 
         if processes is None:
-            try:
-                processes = cpu_count()
-            except NotImplementedError:
-                processes = 1
+            processes = os.cpu_count() or 1
         if processes < 1:
             raise ValueError("Number of processes must be at least 1")
 
diff -r 5e0c56557390 Lib/test/regrtest.py
--- a/Lib/test/regrtest.py	Mon May 20 14:40:46 2013 +0200
+++ b/Lib/test/regrtest.py	Mon May 20 14:52:18 2013 +0200
@@ -508,12 +508,9 @@
         elif o in ('-j', '--multiprocess'):
             use_mp = int(a)
             if use_mp <= 0:
-                try:
-                    import multiprocessing
-                    # Use all cores + extras for tests that like to sleep
-                    use_mp = 2 + multiprocessing.cpu_count()
-                except (ImportError, NotImplementedError):
-                    use_mp = 3
+                # Use all cores + extras for tests that like to sleep
+                ncpu = os.cpu_count() or 1
+                use_mp = 2 + ncpu
             if use_mp == 1:
                 use_mp = None
         elif o == '--header':


More information about the Python-bugs-list mailing list