Skip to content

Instantly share code, notes, and snippets.

@phimachine
Forked from telegraphic/parse_nvidia_smi.py
Created August 7, 2020 06:15
Show Gist options
  • Save phimachine/1af85fbf616dd1d81dbc98bf090b0adf to your computer and use it in GitHub Desktop.
Save phimachine/1af85fbf616dd1d81dbc98bf090b0adf to your computer and use it in GitHub Desktop.
Parse nvidia-smi from python
"""
Parse output of nvidia-smi into a python dictionary.
This is very basic!
"""
import subprocess
import pprint
sp = subprocess.Popen(['nvidia-smi', '-q'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out_str = sp.communicate()
out_list = out_str[0].split('\n')
out_dict = {}
for item in out_list:
try:
key, val = item.split(':')
key, val = key.strip(), val.strip()
out_dict[key] = val
except:
pass
pprint.pprint(out_dict)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment