Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python
- import subprocess
- from xml.etree import ElementTree
- from sys import stderr
- try:
- p = subprocess.Popen(["uname", "-n"],
- stdout=subprocess.PIPE,
- stderr=subprocess.STDOUT)
- p.wait()
- hostname = p.communicate()[0].strip()
- except:
- print >> stderr, "Oops. Couldn't determine my hostname using `uname`"
- exit(1)
- def getUnoccupiedGPUCount():
- result = 0
- try:
- p = subprocess.Popen(["nvidia-smi", "-q", "-x"],
- stdout=subprocess.PIPE,
- stderr=subprocess.STDOUT)
- p.wait()
- xmlt = ElementTree.XML(p.communicate()[0])
- for gpu in xmlt.getiterator(tag="gpu"):
- if len(gpu.find("compute_processes").getchildren()) < 1:
- result += 1
- except:
- result = 0
- print >> stderr, "Oops. Something went wrong, when I tried reading from nvidia-smi. I will exit now!"
- exit(1)
- return result
- while(True):
- cmd = ""
- try:
- cmd = raw_input()
- except:
- pass
- if cmd == "quit":
- exit()
- print "begin\n"+hostname+":gpu:"+str(getUnoccupiedGPUCount())+"\nend"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement