GeezerVison is showing github.com — add os.cpu_count() · Issue #62114 · python/cpython
add os.cpu_count() #62114Description
| BPO |
17914 |
| Nosy |
@pitrou, @vstinner, @giampaolo, @nedbat, @mark-summerfield, @tpn, @ezio-melotti, @bitdancer, @serhiy-storchaka, @kushaldas, @sdrees |
| Files |
issue17914.patch: Patch for adding os.cpu_count()issue17914-1.patch: Modified patch using C implementation of cpu_count()issue17914-2.patchissue17914-3.patch: Modified patch to return None from C codeissue17914-4.patch: Patch modified to use proper macro, remove os.py and skiptest buildbotscpu_count.patchissue17914-5.patch: removed os.py, tyepcasting and redundant comments from previous patchissue17914-6.patch: Change array size to 2, return value to 0(in C code), and removed IRIXissue17914-7.patchuse_cpu_count.diff |
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:
assignee = None
closed_at = <Date 2013-06-28.18:21:53.122>
created_at = <Date 2013-05-06.11:06:47.771>
labels = ['easy', 'type-feature', 'expert-2to3']
title = 'add os.cpu_count()'
updated_at = <Date 2014-12-12.12:36:49.737>
user = 'https://bugs.python.org/neologix'
bugs.python.org fields:
activity = <Date 2014-12-12.12:36:49.737>
actor = 'mark'
assignee = 'none'
closed = True
closed_date = <Date 2013-06-28.18:21:53.122>
closer = 'neologix'
components = ['2to3 (2.x to 3.x conversion tool)']
creation = <Date 2013-05-06.11:06:47.771>
creator = 'neologix'
dependencies = []
files = ['30217', '30220', '30223', '30226', '30236', '30241', '30246', '30282', '30284', '30319']
hgrepos = []
issue_num = 17914
keywords = ['patch', 'easy']
message_count = 60.0
messages = ['188506', '188507', '188509', '188514', '188515', '188516', '188522', '188605', '188606', '188624', '188626', '188627', '188628', '188644', '188905', '188906', '188907', '188909', '188910', '188911', '188912', '188918', '188934', '188944', '188949', '188951', '188953', '188957', '188961', '188963', '188967', '188968', '189053', '189058', '189076', '189079', '189098', '189099', '189104', '189112', '189169', '189170', '189173', '189179', '189180', '189184', '189199', '189360', '189372', '189381', '189654', '189656', '189658', '189670', '189672', '189673', '192005', '232524', '232529', '232541']
nosy_count = 14.0
nosy_names = ['pitrou', 'vstinner', 'giampaolo.rodola', 'nedbat', 'mark', 'trent', 'ezio.melotti', 'r.david.murray', 'neologix', 'python-dev', 'serhiy.storchaka', 'kushal.das', 'dilettant', 'Yogesh.Chaudhari']
pr_nums = []
priority = 'normal'
resolution = 'fixed'
stage = 'resolved'
status = 'closed'
superseder = None
type = 'enhancement'
url = 'https://bugs.python.org/issue17914'
versions = ['Python 3.4']
Activityneologix commented on May 6, 2013 MannequinAuthor More actions multiprocessing.cpu_count() implementation should be made available in the os module, where it belongs. neologix commented on May 6, 2013 MannequinAuthor More actions 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. nedbat commented on May 6, 2013 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. kushaldas commented on May 6, 2013 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). neologix commented on May 6, 2013 MannequinAuthor More actions
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. nedbat commented on May 6, 2013 Seriously, return zero, and I can use it as: cpu_count = os.cpu_count() or 1
Why throw away information? pitrou commented on May 6, 2013 Returning 0 or None sounds better to me than 1 or -1.
(I have a preference for None) vstinner commented on May 6, 2013 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 ;-) vstinner commented on May 6, 2013 See also bpo-17444, Trent Nelson wrote an implementation of os.cpu_count(). neologix commented on May 7, 2013 MannequinAuthor More actions
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)? vstinner commented on May 7, 2013
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. neologix commented on May 7, 2013 MannequinAuthor More actions 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 ;-) ezio-melotti commented on May 7, 2013 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. YogeshChaudhari commented on May 16, 2013 Minor 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 giampaolo commented on May 16, 2013 python-dev commented on May 20, 2013 neologix commented on May 20, 2013 MannequinAuthor More actions 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. vstinner commented on May 20, 2013 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. python-dev commented on May 20, 2013 python-dev commented on May 20, 2013 neologix commented on May 20, 2013 MannequinAuthor More actions
- 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.
Done in a subsequent commit (especially since the OS-X special case
has been removed). python-dev commented on Jun 28, 2013 mark-summerfield commented on Dec 12, 2014 mark-summerfieldmannequin In 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.) vstinner commented on Dec 12, 2014
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-summerfieldmannequin
MetadataMetadataDevelopmentNo branches or pull requestsIssue actions
|