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.

- .. _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 '''

-

def freeze_support(): '''

--- 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):

+ @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

--- 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)

+#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)

+static long +_bsd_cpu_count(void) +{

+

+} +#endif + +static PyObject * +posix_cpu_count(PyObject *self) +{

+#ifdef MS_WINDOWS

+#elif defined(__hpux)

+#elif defined(HAVE_SYSCONF) && defined(_SC_NPROCESSORS_ONLN)

+#elif defined(APPLE)

+#elif defined(DragonFly) || [](#l6.68)

+#endif

+} + 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