"""1CPU Detection2"""3# Parallel Python Software: http://www.parallelpython.com4# Copyright (c) 2005-2008, Vitalii Vanovschi5# All rights reserved.6# Redistribution and use in source and binary forms, with or without7# modification, are permitted provided that the following conditions are met:8# * Redistributions of source code must retain the above copyright notice,9# this list of conditions and the following disclaimer.10# * Redistributions in binary form must reproduce the above copyright11# notice, this list of conditions and the following disclaimer in the12# documentation and/or other materials provided with the distribution.13# * Neither the name of the author nor the names of its contributors14# may be used to endorse or promote products derived from this software15# without specific prior written permission.16#17# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"18# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE19# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE20# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE21# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR22# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF23# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS24# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN25# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)26# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF27# THE POSSIBILITY OF SUCH DAMAGE.2829######30# This is from ParallelPython (the pp.py file).3132import os, subprocess3334def ncpus():35"""36Detects the number of effective CPUs in the system.3738EXAMPLES::3940sage: sage.parallel.ncpus.ncpus() # random output -- depends on machine.41242"""43#for Linux, Unix and MacOS44if hasattr(os, "sysconf"):45if os.sysconf_names.has_key("SC_NPROCESSORS_ONLN"):46#Linux and Unix47ncpus = os.sysconf("SC_NPROCESSORS_ONLN")48if isinstance(ncpus, int) and ncpus > 0:49return ncpus50else:51#MacOS X52#deprecated: return int(os.popen2("sysctl -n hw.ncpu")[1].read())53process = subprocess.Popen("sysctl -n hw.ncpu", shell=True, stdin=subprocess.PIPE, stdout = subprocess.PIPE, stderr=subprocess.PIPE, close_fds=True)54return int(process.stdout.read())55#for Windows56if os.environ.has_key("NUMBER_OF_PROCESSORS"):57ncpus = int(os.environ["NUMBER_OF_PROCESSORS"])58if ncpus > 0:59return ncpus60#return the default value61return 1626364