@@ -109,32 +109,23 @@ def to_unicode(s):
109
109
return s
110
110
111
111
112
- # TODO(yln): multiprocessing.cpu_count()
113
- # TODO(python3): len(os.sched_getaffinity(0)) and os.cpu_count()
114
- def detectCPUs ():
115
- """Detects the number of CPUs on a system.
116
-
117
- Cribbed from pp.
112
+ def usable_core_count ():
113
+ """Return the number of cores the current process can use, if supported.
114
+ Otherwise, return the total number of cores (like `os.cpu_count()`).
115
+ Default to 1 if undetermined.
118
116
119
117
"""
120
- # Linux, Unix and MacOS:
121
- if hasattr (os , 'sysconf' ):
122
- if 'SC_NPROCESSORS_ONLN' in os .sysconf_names :
123
- # Linux & Unix:
124
- ncpus = os .sysconf ('SC_NPROCESSORS_ONLN' )
125
- if isinstance (ncpus , int ) and ncpus > 0 :
126
- return ncpus
127
- else : # OSX:
128
- return int (subprocess .check_output (['sysctl' , '-n' , 'hw.ncpu' ],
129
- stderr = subprocess .STDOUT ))
130
- # Windows:
131
- if 'NUMBER_OF_PROCESSORS' in os .environ :
132
- ncpus = int (os .environ ['NUMBER_OF_PROCESSORS' ])
133
- if ncpus > 0 :
134
- # With more than 32 processes, process creation often fails with
135
- # "Too many open files". FIXME: Check if there's a better fix.
136
- return min (ncpus , 32 )
137
- return 1 # Default
118
+ try :
119
+ n = len (os .sched_getaffinity (0 ))
120
+ except AttributeError :
121
+ n = os .cpu_count () or 1
122
+
123
+ # On Windows, with more than 32 processes, process creation often fails with
124
+ # "Too many open files". FIXME: Check if there's a better fix.
125
+ if platform .system () == 'Windows' :
126
+ return min (n , 32 )
127
+
128
+ return n
138
129
139
130
140
131
def mkdir (path ):
0 commit comments