Skip to content

Commit

Permalink
Improve the handling of non-existent PID to be more robust
Browse files Browse the repository at this point in the history
Since it'd better to not ignore CalledProcessError on calling ps,
we let it exit with a non-zero return code in the corner cases
in which nvidia-smi might return non-existent process PID.

For details, see #12.
  • Loading branch information
wookayin committed Aug 1, 2017
1 parent 3cb1400 commit f66cffd
Showing 1 changed file with 13 additions and 9 deletions.
22 changes: 13 additions & 9 deletions gpustat.py
Original file line number Diff line number Diff line change
Expand Up @@ -267,18 +267,22 @@ def running_processes():

# 2. map pid to username, etc.
if pid_map:
try:
pid_output = execute_process('ps -o {} -p {}'.format(
'pid,user:16,comm',
','.join(map(str, pid_map.keys()))
))
except CalledProcessError:
# Exception on PID of non-existent process returned by nvidia-smi
pid_output = ""
# Sometimes nvidia-smi returns non-existent process PID (see #12);
# To let ps exit with a non-zero return code in such cases,
# we always include querying PID 1 as well (but ignored).
pid_output = execute_process('ps -o {} -p1 -p {}'.format(
'pid,user:16,comm',
','.join(map(str, pid_map.keys()))
))

for line in pid_output.split('\n'):
if (not line) or 'PID' in line: continue
pid, user, comm = line.split()[:3]
pid_map[int(pid)] = {

pid = int(pid)
if pid <= 1: continue

pid_map[pid] = {
'user' : user,
'comm' : comm
}
Expand Down

0 comments on commit f66cffd

Please sign in to comment.