pprocess

Changeset

175:eaaedc7eaf62
19 months ago Paul Boddie raw files shortlog changelog graph Added support for counting CPU cores on Mac OS X.
pprocess.py (file)
     1.1 --- a/pprocess.py	Mon Dec 19 17:53:25 2016 +0100
     1.2 +++ b/pprocess.py	Mon Dec 19 17:55:21 2016 +0100
     1.3 @@ -1050,6 +1050,24 @@
     1.4      finally:
     1.5          f.close()
     1.6  
     1.7 +_system_profiler_field = "Total Number of Cores:"
     1.8 +
     1.9 +def _get_number_of_cores_macosx():
    1.10 +
    1.11 +    "Return the number of cores for Mac OS X."
    1.12 +
    1.13 +    f = os.popen("/usr/sbin/system_profiler -detailLevel full SPHardwareDataType")
    1.14 +    try:
    1.15 +        for line in f.xreadlines():
    1.16 +            line = line.strip()
    1.17 +            if line.startswith(_system_profiler_field):
    1.18 +                return int(line[len(_system_profiler_field):].strip())
    1.19 +
    1.20 +        return None
    1.21 +
    1.22 +    finally:
    1.23 +        f.close()
    1.24 +
    1.25  # Low-level functions.
    1.26  
    1.27  def create_socketpair():
    1.28 @@ -1094,9 +1112,14 @@
    1.29          os.close(cw)
    1.30          return Channel(pid, os.fdopen(pr, "r", 0), os.fdopen(pw, "w", 0))
    1.31  
    1.32 +# Configure the interprocess communications and core-counting functions.
    1.33 +
    1.34  if platform.system() == "SunOS":
    1.35      create = create_pipes
    1.36      get_number_of_cores = _get_number_of_cores_solaris
    1.37 +elif platform.system() == "Darwin":
    1.38 +    create = create_socketpair
    1.39 +    get_number_of_cores = _get_number_of_cores_macosx
    1.40  else:
    1.41      create = create_socketpair
    1.42      get_number_of_cores = _get_number_of_cores