Created
July 12, 2016 15:38
-
-
Save ryanwoodsmall/17caab065ddfca5aca740f9ec5f11d22 to your computer and use it in GitHub Desktop.
get CPU counts and memory in bytes for Python on Linux
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
def get_cpu_count(): | |
cpu_count = 0 | |
cpu_file = '/proc/cpuinfo' | |
ch = open(cpu_file, 'r') | |
for line in ch: | |
if re.match('processor', line): | |
cpu_count = cpu_count + 1 | |
ch.close() | |
return(cpu_count) | |
def get_mem_bytes(): | |
mem_bytes = 0 | |
mem_file = '/proc/meminfo' | |
mh = open(mem_file, 'r') | |
for line in mh: | |
if re.match('MemTotal', line): | |
mem_bytes = int(line.split()[1]) * 1024 | |
mh.close() | |
return(mem_bytes) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment