[issue17914] add os.cpu_count()

STINNER Victor report at bugs.python.org
Mon May 13 01:56:12 CEST 2013


STINNER Victor added the comment:

Here is a new patch (cpu_count.patch) with a different approach:

 * add a os.cpu_count() which returns the raw value (may be zero or negative if the OS returns a dummy value) and raise an OSError on error
 * os.cpu_count() is not available on all platforms
 * shutil.cpu_count() is the high level API using 1 as a fallback. The fallback value (which is 1 by default) is configurable, ex: shutil.cpu_count(fallback=None) returns None on os.cpu_count() error.
 * multiprocessing.cpu_count() simply reuses shutil.cpu_count()

So os.cpu_count() as a well defined behaviour, and shutil.cpu_count() is the convinient API.

My patch is based on issue17914-4.patch and so also on Trent Nelson's code.

I only tested my patch on Linux. It must be tested on other platforms. If nobody tests the patch on HPUX, it would be safer to remove HPUX support.

It looks like Trent's code was not tested, I don't think that his code works on platforms other than Windows.

test_os will fail if os.cpu_count() fails. The test should be fixed to handle failures, but I prefer to start with a failing test to check if the error case occurs on a buildbot.

----------
Added file: http://bugs.python.org/file30241/cpu_count.patch

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue17914>
_______________________________________


More information about the Python-bugs-list mailing list