Skip to content

Instantly share code, notes, and snippets.

@maligree
Created September 5, 2013 10:33
Show Gist options
  • Select an option

  • Save maligree/6448505 to your computer and use it in GitHub Desktop.

Select an option

Save maligree/6448505 to your computer and use it in GitHub Desktop.
kod z multiprocessing
def cpu_count():
'''
Returns the number of CPUs in the system
'''
if sys.platform == 'win32':
try:
num = int(os.environ['NUMBER_OF_PROCESSORS'])
except (ValueError, KeyError):
num = 0
elif 'bsd' in sys.platform or sys.platform == 'darwin':
comm = '/sbin/sysctl -n hw.ncpu'
if sys.platform == 'darwin':
comm = '/usr' + comm
try:
with os.popen(comm) as p:
num = int(p.read())
except ValueError:
num = 0
else:
try:
num = os.sysconf('SC_NPROCESSORS_ONLN')
except (ValueError, OSError, AttributeError):
num = 0
if num >= 1:
return num
else:
raise NotImplementedError('cannot determine number of cpus')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment