more robust cpu detection

This commit is contained in:
Tully Foote 2009-10-28 23:54:32 +00:00
parent ce9ab3fb5d
commit 553f49eed2
1 changed files with 18 additions and 9 deletions

View File

@ -47,15 +47,24 @@ import threading
def num_cpus(): def num_cpus():
n = 0 """
cpuinfo_file = "/proc/cpuinfo" Detects the number of CPUs on a system. Cribbed from pp.
if os.path.exists(cpuinfo_file): """
for line in open(cpuinfo_file): # Linux, Unix and MacOS:
if line.startswith("processor"): if hasattr(os, "sysconf"):
n = n + 1 if os.sysconf_names.has_key("SC_NPROCESSORS_ONLN"):
else: # Linux & Unix:
n = 1 ncpus = os.sysconf("SC_NPROCESSORS_ONLN")
return n if isinstance(ncpus, int) and ncpus > 0:
return ncpus
else: # OSX:
return int(os.popen2("sysctl -n hw.ncpu")[1].read())
# Windows:
if os.environ.has_key("NUMBER_OF_PROCESSORS"):
ncpus = int(os.environ["NUMBER_OF_PROCESSORS"]);
if ncpus > 0:
return ncpus
return 1 # Default
class DependencyTracker: class DependencyTracker:
""" Track dependencies between packages. This is basically a """ Track dependencies between packages. This is basically a