-
Notifications
You must be signed in to change notification settings - Fork 1
/
cpucorecount.py
48 lines (41 loc) · 1.03 KB
/
cpucorecount.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import os
import subprocess as subp
'''
Internal implementation functions and variables
'''
_core_count_cached = None
def _parseWMICOutput(output):
global _core_count_cached
for line in output:
line = line.strip()
if not line:
continue
line = line.split('=')
if line[0].strip().lower() == 'numberofcores':
_core_count_cached = int(line[1].strip())
break
if _core_count_cached == None:
_core_count_cached = -1
return
def _getWindowsCPUPhysicalCoreCount():
p = subp.Popen(['wmic', 'CPU', 'Get', '/Format:List'], stdout=subp.PIPE, stderr=subp.PIPE)
ret = p.wait()
if ret != 0:
global _core_count_cached
_core_count_cached = -1
else:
_parseWMICOutput(p.stdout.readlines())
return
'''
The public API to get the physical core count
'''
def getCPUPhysicalCoreCount():
if _core_count_cached != None:
return _core_count_cached
if os.name == 'nt':
_getWindowsCPUPhysicalCoreCount()
else:
raise OSError('Unsupported OS: ' + os.name)
return _core_count_cached
if __name__ == '__main__':
print getCPUPhysicalCoreCount()