Return the effective number of CPUs in the system as an integer. This cross-platform function makes an attempt at finding the total number of available CPUs in the system, as returned by various underlying system and python calls. If it can't find a sensible answer, it returns 1 (thoug
()
| 135 | |
| 136 | |
| 137 | def num_cpus(): |
| 138 | """Return the effective number of CPUs in the system as an integer. |
| 139 | |
| 140 | This cross-platform function makes an attempt at finding the total number of |
| 141 | available CPUs in the system, as returned by various underlying system and |
| 142 | python calls. |
| 143 | |
| 144 | If it can't find a sensible answer, it returns 1 (though an error *may* make |
| 145 | it return a large positive number that's actually incorrect). |
| 146 | """ |
| 147 | |
| 148 | # Many thanks to the Parallel Python project (http://www.parallelpython.com) |
| 149 | # for the names of the keys we needed to look up for this function. This |
| 150 | # code was inspired by their equivalent function. |
| 151 | |
| 152 | ncpufuncs = {'Linux':_num_cpus_unix, |
| 153 | 'Darwin':_num_cpus_darwin, |
| 154 | 'Windows':_num_cpus_windows |
| 155 | } |
| 156 | |
| 157 | ncpufunc = ncpufuncs.get(platform.system(), |
| 158 | # default to unix version (Solaris, AIX, etc) |
| 159 | _num_cpus_unix) |
| 160 | |
| 161 | try: |
| 162 | ncpus = max(1,int(ncpufunc())) |
| 163 | except: |
| 164 | ncpus = 1 |
| 165 | return ncpus |
| 166 |