Repository navigation
add os.cpu_count() #62114
Description
Activity
multiprocessing.cpu_count() implementation should be made available in the os module, where it belongs.
- addedtype-featureA feature request or enhancementA feature request or enhancement
on May 6, 2013 Note that I think it might be interesting to return 1 if the actual value cannot be determined, since that's a sensible default, and likely what the caller will do anyway. This contrasts with the current multiprocessing's implementation which raised NotImplementedError.
If you can't determine the number of CPUs, return a clear "can't determine" value, such as 0 or -1. Returning 1 will hide information, and it's an easy default for the caller to apply if they want to.
I am interested to submit a patch on this. Should I move the implementation to os module and made the multiprocessing one as an alias ? or keep it in both places ?
I prefer the idea of returning -1 instead of the current way of raising NotImplementedError in case we can not determine the number of CPU(s).
I am interested to submit a patch on this. Should I move the implementation to os module and made the multiprocessing one as an alias ? or keep it in both places ?
Yes, you should move it, add a corresponding documentation to
Doc/modules/os.rst (you can probably reuse the multiprocessing doc),
and add a test in Lib/test/test_os.py (you can also probably reuse the
multiprocessing test).I prefer the idea of returning -1 instead of the current way of raising NotImplementedError in case we can not determine the number of CPU(s).
Seriously, I don't see what this brings. Since the user can't do
anything except using 1 instead, why not do this in the library?
I've searched a bit, and other platforms (.e.g Java, Ruby) don't raise
an exception, and always return a positive value.Seriously, return zero, and I can use it as: cpu_count = os.cpu_count() or 1
Why throw away information?
Returning 0 or None sounds better to me than 1 or -1.
(I have a preference for None)I also vote +1 for returning None when the information is unknown.
Just write "os.cpu_count() or 1" if you need 1 when the count is unknown ;-)
See also bpo-17444, Trent Nelson wrote an implementation of os.cpu_count().
I also vote +1 for returning None when the information is unknown.
I still don't like it.
If a function returns a number of CPU, it should either return an
integer >= 1, or raise an exception.
None is *not* an integer.And returning an exception is IMO useles, since the user can't do
anything with anyway, other than fallback to 1.Just write "os.cpu_count() or 1" if you need 1 when the count is unknown ;-)
os.cpu_count() or 1 is an ugly idiom.
See also bpo-17444, Trent Nelson wrote an implementation of os.cpu_count().
I don't see exactly what this C implementation brings over the one in
multiprocessing (which is written in Python)?I don't see exactly what this C implementation brings over the one
in multiprocessing (which is written in Python)?multiprocessing.cpu_count() creates a subprocess on BSD and Darwin to get the number of CPU. Calling sysctl() or sysctlnametomib() should be faster and use less memory.
On Windows, GetSystemInfo() is called instead of reading an environment variable. I suppose that this function is more reliable.
Trent's os.cpu_count() returns -1 if the count cannot be read, multiprocessing.cpu_count() raises NotImplementedError.
Fair enough, I guess we should use it then.
We just have to agree on the value to return when the number of CPUs
can't be determined ;-)Returning None sounds reasonable to me.
Raising an exception pretty much means that the function should always be called in a try/except (unless you are sure that the code is running on an OS that knows the number of CPUs). Returning -1 is not very Pythonic, and between 0 and None I prefer the latter, since it's IMHO a clearer indication that the value couldn't be determined.35 remaining items
YogeshChaudhari commented
on May 16, 2013 YogeshChaudharimannequinMannequinMore actionsMinor modifications based on review comments.
- Change mib array size to 2,
- return value set to 0 consistently (in C code), and
- removed IRIX #defines
YogeshChaudhari commented
on May 16, 2013 YogeshChaudharimannequinMannequinMore actionsTypo fix
+1 for returning None.
I haven't looked into patches but if needed feel free to borrow some code from psutil:New changeset 5e0c56557390 by Charles-Francois Natali in branch 'default':
Issue bpo-17914: Add os.cpu_count(). Patch by Yogesh Chaudhari, based on an
http://hg.python.org/cpython/rev/5e0c56557390Alright, 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.In my patch cpu_count.patch, I changed posix_cpu_count():
- rewrite Mac OS X implementation: code in 5e0c56557390 looks wrong. It gets a MIB but then don't use it when calling _bsd_cpu_count(). But I didn't check my patch nor the commit version on Mac OS X.
- use "int ncpu;" instead of "long ncpu;" when calling mpctl() and sysctl(). For mpctl(), ncpu is used to store the result, so a wide type is ok. But for sysctl(), we pass a pointer. What happens if sysctl() expects int whereas we pass a pointer to a long? We announce sizeof(int)!?
- inline _bsd_cpu_count()
Sorry for this late review.
New changeset a85ac58e9eaf by Charles-Francois Natali in branch 'default':
Issue bpo-17914: Remove OS-X special-case, and use the correct int type.
http://hg.python.org/cpython/rev/a85ac58e9eafNew changeset f9d815522cdb by Charles-Francois Natali in branch 'default':
Issue bpo-17914: We can now inline _bsd_cpu_count().
http://hg.python.org/cpython/rev/f9d815522cdb- rewrite Mac OS X implementation: code in 5e0c56557390 looks wrong. It
gets a MIB but then don't use it when calling _bsd_cpu_count(). But I didn't
check my patch nor the commit version on Mac OS X.
Indeed.
I just removed the OS-X special case altogether.
Apparently, the standard sysctl is supposed to work os OS-X.
We'll see what happens.- use "int ncpu;" instead of "long ncpu;" when calling mpctl() and
sysctl(). For mpctl(), ncpu is used to store the result, so a wide type is
ok. But for sysctl(), we pass a pointer. What happens if sysctl() expects
int whereas we pass a pointer to a long? We announce sizeof(int)!?
Ouch. This was overlooked when the type was changed to long.
I fixed this.- inline _bsd_cpu_count()
Done in a subsequent commit (especially since the OS-X special case
has been removed).- rewrite Mac OS X implementation: code in 5e0c56557390 looks wrong. It
New changeset 6a0437adafbd by Charles-François Natali in branch 'default':
Issue bpo-17914: Use os.cpu_count() instead of multiprocessing.cpu_count() where
http://hg.python.org/cpython/rev/6a0437adafbdmark-summerfield commented
on Dec 12, 2014 mark-summerfieldmannequinMannequinMore actionsIn message
http://bugs.python.org/issue17914#msg188626
Victor Stenner says"On Windows, GetSystemInfo() is called instead of reading an environment variable. I suppose that this function is more reliable."
From my reading, and based on feedback from one of my customers, I believe he is correct and that GetSystemInfo() ought to be used on Windows. (It is available in pywin32 win32api.)
From my reading, and based on feedback from one of my customers, I believe he is correct and that GetSystemInfo() ought to be used on Windows. (It is available in pywin32 win32api.)
Please open a new issue to suggest this enhancement, this issue is closed.
mark-summerfield commented
on Dec 12, 2014 mark-summerfieldmannequinMannequinMore actionsSince this is closed I've created a new issue as requested:
http://bugs.python.org/issue23037
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields: