Skip to content

Instantly share code, notes, and snippets.

@ryanwoodsmall
Created July 12, 2016 15:38
Show Gist options
  • Save ryanwoodsmall/17caab065ddfca5aca740f9ec5f11d22 to your computer and use it in GitHub Desktop.
Save ryanwoodsmall/17caab065ddfca5aca740f9ec5f11d22 to your computer and use it in GitHub Desktop.
get CPU counts and memory in bytes for Python on Linux
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