cpython: 5e0c56557390 (original) (raw)
Mercurial > cpython
changeset 83858:5e0c56557390
Issue #17914: Add os.cpu_count(). Patch by Yogesh Chaudhari, based on an initial patch by Trent Nelson. [#17914]
Charles-Francois Natali cf.natali@gmail.com | |
---|---|
date | Mon, 20 May 2013 14:40:46 +0200 |
parents | 42dda22a6f8c |
children | 20409786cf8e |
files | Doc/library/multiprocessing.rst Doc/library/os.rst Lib/multiprocessing/__init__.py Lib/test/test_os.py Misc/NEWS Modules/posixmodule.c |
diffstat | 6 files changed, 94 insertions(+), 26 deletions(-)[+] [-] Doc/library/multiprocessing.rst 3 Doc/library/os.rst 11 Lib/multiprocessing/__init__.py 25 Lib/test/test_os.py 10 Misc/NEWS 3 Modules/posixmodule.c 68 |
line wrap: on
line diff
--- a/Doc/library/multiprocessing.rst
+++ b/Doc/library/multiprocessing.rst
@@ -702,6 +702,9 @@ Miscellaneous
Return the number of CPUs in the system. May raise
:exc:NotImplementedError
.
+
.. function:: current_process()
Return the :class:Process
object corresponding to the current process.
--- a/Doc/library/os.rst +++ b/Doc/library/os.rst @@ -3155,10 +3155,6 @@ operating system. Return the set of CPUs the process with PID pid (or the current process if zero) is restricted to.
- .. seealso::
:func:`multiprocessing.cpu_count` returns the number of CPUs in the[](#l2.8)
system.[](#l2.9)
- .. _os-path: @@ -3196,6 +3192,13 @@ Miscellaneous System Information Availability: Unix. +.. function:: cpu_count() +
+ .. function:: getloadavg() Return the number of processes in the system run queue averaged over the last
--- a/Lib/multiprocessing/init.py +++ b/Lib/multiprocessing/init.py @@ -85,30 +85,11 @@ def cpu_count(): ''' Returns the number of CPUs in the system '''
- if sys.platform == 'win32':
try:[](#l3.8)
num = int(os.environ['NUMBER_OF_PROCESSORS'])[](#l3.9)
except (ValueError, KeyError):[](#l3.10)
num = 0[](#l3.11)
- elif 'bsd' in sys.platform or sys.platform == 'darwin':
comm = '/sbin/sysctl -n hw.ncpu'[](#l3.13)
if sys.platform == 'darwin':[](#l3.14)
comm = '/usr' + comm[](#l3.15)
try:[](#l3.16)
with os.popen(comm) as p:[](#l3.17)
num = int(p.read())[](#l3.18)
except ValueError:[](#l3.19)
num = 0[](#l3.20)
- num = os.cpu_count()
- if num is None:
else:raise NotImplementedError('cannot determine number of cpus')[](#l3.23)
try:[](#l3.25)
num = os.sysconf('SC_NPROCESSORS_ONLN')[](#l3.26)
except (ValueError, OSError, AttributeError):[](#l3.27)
num = 0[](#l3.28)
- if num >= 1: return num
- else:
raise NotImplementedError('cannot determine number of cpus')[](#l3.33)
--- a/Lib/test/test_os.py +++ b/Lib/test/test_os.py @@ -2216,6 +2216,15 @@ class OSErrorTests(unittest.TestCase): else: self.fail("No exception thrown by {}".format(func)) +class CPUCountTests(unittest.TestCase):
- def test_cpu_count(self):
cpus = os.cpu_count()[](#l4.9)
if cpus is not None:[](#l4.10)
self.assertIsInstance(cpus, int)[](#l4.11)
self.assertGreater(cpus, 0)[](#l4.12)
else:[](#l4.13)
self.skipTest("Could not determine the number of CPUs")[](#l4.14)
+ @support.reap_threads def test_main(): support.run_unittest( @@ -2246,6 +2255,7 @@ def test_main(): TermsizeTests, OSErrorTests, RemoveDirsTests,
--- a/Misc/NEWS +++ b/Misc/NEWS @@ -99,6 +99,9 @@ Core and Builtins Library ------- +- Issue #17914: Add os.cpu_count(). Patch by Yogesh Chaudhari, based on an
- Issue #17980: Fix possible abuse of ssl.match_hostname() for denial of service using certificates with many wildcards (CVE-2013-2099).
--- a/Modules/posixmodule.c +++ b/Modules/posixmodule.c @@ -113,6 +113,18 @@ corresponding Unix manual entries for mo #include <dlfcn.h> #endif +#ifdef __hpux +#include <sys/mpctl.h> +#endif + +#if defined(DragonFly) || [](#l6.11)
- defined(OpenBSD) || [](#l6.12)
- defined(FreeBSD) || [](#l6.13)
- defined(NetBSD) || [](#l6.14)
- defined(APPLE)
+#include <sys/sysctl.h> +#endif + #if defined(MS_WINDOWS)
define TERMSIZE_USE_CONIO
#elif defined(HAVE_SYS_IOCTL_H) @@ -10302,6 +10314,60 @@ get_terminal_size(PyObject self, PyObje } #endif / defined(TERMSIZE_USE_CONIO) || defined(TERMSIZE_USE_IOCTL) */ +PyDoc_STRVAR(posix_cpu_count__doc__, +"cpu_count() -> integer\n\n[](#l6.27) +Return the number of CPUs in the system, or None if this value cannot be\n[](#l6.28) +established."); + +#if defined(DragonFly) || [](#l6.31)
- defined(OpenBSD) || [](#l6.32)
- defined(FreeBSD) || [](#l6.33)
- defined(NetBSD) || [](#l6.34)
- defined(APPLE)
+static long +_bsd_cpu_count(void) +{
- mib[0] = CTL_HW;
- mib[1] = HW_NCPU;
- if (sysctl(mib, 2, &ncpu, &len, NULL, 0) == 0)
return ncpu;[](#l6.46)
- else
return 0;[](#l6.48)
+} +#endif + +static PyObject * +posix_cpu_count(PyObject *self) +{
+#elif defined(HAVE_SYSCONF) && defined(_SC_NPROCESSORS_ONLN)
- size_t len = sizeof(int);
- if (sysctlnametomib("hw.logicalcpu", &ncpu, &len, NULL, 0) != 0)
ncpu = _bsd_cpu_count();[](#l6.67)
+#elif defined(DragonFly) || [](#l6.68)
defined(__OpenBSD__) || \[](#l6.69)
defined(__FreeBSD__) || \[](#l6.70)
defined(__NetBSD__)[](#l6.71)
- ncpu = _bsd_cpu_count();
+} + static PyMethodDef posix_methods[] = { {"access", (PyCFunction)posix_access, @@ -10747,6 +10813,8 @@ static PyMethodDef posix_methods[] = { #if defined(TERMSIZE_USE_CONIO) || defined(TERMSIZE_USE_IOCTL) {"get_terminal_size", get_terminal_size, METH_VARARGS, termsize__doc__}, #endif