Python os 模块,sysconf_names() 实例源码
我们从Python开源项目中,提取了以下11个代码示例,用于说明如何使用os.sysconf_names()。
def _detect_ncpus():
"""Detect the number of effective CPUs in the system"""
# Snippet taken from ParallelPython
# For Linux, Unix and MacOS
if hasattr(os, "sysconf"):
if "SC_NPROCESSORS_ONLN" in os.sysconf_names:
#Linux and Unix
ncpus = os.sysconf("SC_NPROCESSORS_ONLN")
if isinstance(ncpus, int) and ncpus > 0:
return ncpus
else:
#MacOS X
return int(os.popen2("sysctl -n hw.ncpu")[1].read())
#for Windows
if "NUMBER_OF_PROCESSORS" in os.environ:
ncpus = int(os.environ["NUMBER_OF_PROCESSORS"])
if ncpus > 0:
return ncpus
#return the default value
return 1
def _detect_ncpus():
"""Detect the number of effective CPUs in the system"""
# Snippet taken from ParallelPython
# For Linux, Unix and MacOS
if hasattr(os, "sysconf"):
if "SC_NPROCESSORS_ONLN" in os.sysconf_names:
#Linux and Unix
ncpus = os.sysconf("SC_NPROCESSORS_ONLN")
if isinstance(ncpus, int) and ncpus > 0:
return ncpus
else:
#MacOS X
return int(os.popen2("sysctl -n hw.ncpu")[1].read())
#for Windows
if "NUMBER_OF_PROCESSORS" in os.environ:
ncpus = int(os.environ["NUMBER_OF_PROCESSORS"])
if ncpus > 0:
return ncpus
#return the default value
return 1
def detect_number_of_cores():
"""
Detects the number of cores on a system. Cribbed from pp.
"""
# Linux, Unix and MacOS:
if hasattr(os, "sysconf"):
if "SC_NPROCESSORS_ONLN" in os.sysconf_names:
# Linux & Unix:
ncpus = os.sysconf("SC_NPROCESSORS_ONLN")
if isinstance(ncpus, int) and ncpus > 0:
return ncpus
else: # OSX:
return int(subprocess.check_output(["sysctl", "-n", "hw.ncpu"]))
# Windows:
if "NUMBER_OF_PROCESSORS" in os.environ:
ncpus = int(os.environ["NUMBER_OF_PROCESSORS"]);
if ncpus > 0:
return ncpus
return 1 # Default
def detectCPUs():
"""
Detects the number of CPUs on a system. Cribbed from pp.
"""
# Linux, Unix and MacOS:
if hasattr(os, "sysconf"):
if "SC_NPROCESSORS_ONLN" in os.sysconf_names:
# Linux & Unix:
ncpus = os.sysconf("SC_NPROCESSORS_ONLN")
if isinstance(ncpus, int) and ncpus > 0:
return ncpus
else: # OSX:
return int(capture(['sysctl', '-n', 'hw.ncpu']))
# Windows:
if "NUMBER_OF_PROCESSORS" in os.environ:
ncpus = int(os.environ["NUMBER_OF_PROCESSORS"])
if ncpus > 0:
return ncpus
return 1 # Default
def jobs(self):
"""
Find the amount of cpu cores to set the default amount of tasks executed in parallel. At
runtime the options can be obtained from :py:const:`waflib.Options.options` ::
from waflib.Options import options
njobs = options.jobs
:return: the amount of cpu cores
:rtype: int
"""
count = int(os.environ.get('JOBS', 0))
if count < 1:
if 'NUMBER_OF_PROCESSORS' in os.environ:
# on Windows, use the NUMBER_OF_PROCESSORS environment variable
count = int(os.environ.get('NUMBER_OF_PROCESSORS', 1))
else:
# on everything else, first try the POSIX sysconf values
if hasattr(os, 'sysconf_names'):
if 'SC_NPROCESSORS_ONLN' in os.sysconf_names:
count = int(os.sysconf('SC_NPROCESSORS_ONLN'))
elif 'SC_NPROCESSORS_CONF' in os.sysconf_names:
count = int(os.sysconf('SC_NPROCESSORS_CONF'))
if not count and os.name not in ('nt', 'java'):
try:
tmp = self.cmd_and_log(['sysctl', '-n', 'hw.ncpu'], quiet=0)
except Exception:
pass
else:
if re.match('^[0-9]+$', tmp):
count = int(tmp)
if count < 1:
count = 1
elif count > 1024:
count = 1024
return count
def jobs(self):
"""
Find the amount of cpu cores to set the default amount of tasks executed in parallel. At
runtime the options can be obtained from :py:const:`waflib.Options.options` ::
from waflib.Options import options
njobs = options.jobs
:return: the amount of cpu cores
:rtype: int
"""
count = int(os.environ.get('JOBS', 0))
if count < 1:
if 'NUMBER_OF_PROCESSORS' in os.environ:
# on Windows, use the NUMBER_OF_PROCESSORS environment variable
count = int(os.environ.get('NUMBER_OF_PROCESSORS', 1))
else:
# on everything else, first try the POSIX sysconf values
if hasattr(os, 'sysconf_names'):
if 'SC_NPROCESSORS_ONLN' in os.sysconf_names:
count = int(os.sysconf('SC_NPROCESSORS_ONLN'))
elif 'SC_NPROCESSORS_CONF' in os.sysconf_names:
count = int(os.sysconf('SC_NPROCESSORS_CONF'))
if not count and os.name not in ('nt', 'java'):
try:
tmp = self.cmd_and_log(['sysctl', '-n', 'hw.ncpu'], quiet=0)
except Exception:
pass
else:
if re.match('^[0-9]+$', tmp):
count = int(tmp)
if count < 1:
count = 1
elif count > 1024:
count = 1024
return count
def jobs(self):
"""
Find the amount of cpu cores to set the default amount of tasks executed in parallel. At
runtime the options can be obtained from :py:const:`waflib.Options.options` ::
from waflib.Options import options
njobs = options.jobs
:return: the amount of cpu cores
:rtype: int
"""
count = int(os.environ.get('JOBS', 0))
if count < 1:
if 'NUMBER_OF_PROCESSORS' in os.environ:
# on Windows, use the NUMBER_OF_PROCESSORS environment variable
count = int(os.environ.get('NUMBER_OF_PROCESSORS', 1))
else:
# on everything else, first try the POSIX sysconf values
if hasattr(os, 'sysconf_names'):
if 'SC_NPROCESSORS_ONLN' in os.sysconf_names:
count = int(os.sysconf('SC_NPROCESSORS_ONLN'))
elif 'SC_NPROCESSORS_CONF' in os.sysconf_names:
count = int(os.sysconf('SC_NPROCESSORS_CONF'))
if not count and os.name not in ('nt', 'java'):
try:
tmp = self.cmd_and_log(['sysctl', '-n', 'hw.ncpu'], quiet=0)
except Exception:
pass
else:
if re.match('^[0-9]+$', tmp):
count = int(tmp)
if count < 1:
count = 1
elif count > 1024:
count = 1024
return count
def NumLocalCpus():
"""Returns the number of processors.
multiprocessing.cpu_count() is permitted to raise NotImplementedError, and
is known to do this on some Windows systems and OSX 10.6. If we can't get the
CPU count, we will fall back to '1'.
"""
# Surround the entire thing in try/except; no failure here should stop gclient
# from working.
try:
# Use multiprocessing to get CPU count. This may raise
# NotImplementedError.
try:
import multiprocessing
return multiprocessing.cpu_count()
except NotImplementedError: # pylint: disable=bare-except
# (UNIX) Query 'os.sysconf'.
# pylint: disable=no-member
if hasattr(os, 'sysconf') and 'SC_NPROCESSORS_ONLN' in os.sysconf_names:
return int(os.sysconf('SC_NPROCESSORS_ONLN'))
# (Windows) Query 'NUMBER_OF_PROCESSORS' environment variable.
if 'NUMBER_OF_PROCESSORS' in os.environ:
return int(os.environ['NUMBER_OF_PROCESSORS'])
except Exception as e:
logging.exception("Exception raised while probing CPU count: %s", e)
logging.debug('Failed to get CPU count. Defaulting to 1.')
return 1
def NumLocalCpus():
"""Returns the number of processors.
multiprocessing.cpu_count() is permitted to raise NotImplementedError, and
is known to do this on some Windows systems and OSX 10.6. If we can't get the
CPU count, we will fall back to '1'.
"""
# Surround the entire thing in try/except; no failure here should stop gclient
# from working.
try:
# Use multiprocessing to get CPU count. This may raise
# NotImplementedError.
try:
import multiprocessing
return multiprocessing.cpu_count()
except NotImplementedError: # pylint: disable=bare-except
# (UNIX) Query 'os.sysconf'.
# pylint: disable=no-member
if hasattr(os, 'sysconf') and 'SC_NPROCESSORS_ONLN' in os.sysconf_names:
return int(os.sysconf('SC_NPROCESSORS_ONLN'))
# (Windows) Query 'NUMBER_OF_PROCESSORS' environment variable.
if 'NUMBER_OF_PROCESSORS' in os.environ:
return int(os.environ['NUMBER_OF_PROCESSORS'])
except Exception as e:
logging.exception("Exception raised while probing CPU count: %s", e)
logging.debug('Failed to get CPU count. Defaulting to 1.')
return 1
def get_sys_info():
"""
Return dictionary of system information
SC_NPROCESSORS_ONLN returns the number of processors which are currently
online (i.e. available). See os.sysconf_names for dictionary values.
"""
return {
"nodename": gethostname(),
"total_logical_cpus": os.sysconf(84)
}
def NumLocalCpus():
"""Returns the number of processors.
multiprocessing.cpu_count() is permitted to raise NotImplementedError, and
is known to do this on some Windows systems and OSX 10.6. If we can't get the
CPU count, we will fall back to '1'.
"""
# Surround the entire thing in try/except; no failure here should stop gclient
# from working.
try:
# Use multiprocessing to get CPU count. This may raise
# NotImplementedError.
try:
import multiprocessing
return multiprocessing.cpu_count()
except NotImplementedError: # pylint: disable=bare-except
# (UNIX) Query 'os.sysconf'.
# pylint: disable=no-member
if hasattr(os, 'sysconf') and 'SC_NPROCESSORS_ONLN' in os.sysconf_names:
return int(os.sysconf('SC_NPROCESSORS_ONLN'))
# (Windows) Query 'NUMBER_OF_PROCESSORS' environment variable.
if 'NUMBER_OF_PROCESSORS' in os.environ:
return int(os.environ['NUMBER_OF_PROCESSORS'])
except Exception as e:
logging.exception("Exception raised while probing CPU count: %s", e)
logging.debug('Failed to get CPU count. Defaulting to 1.')
return 1