mirror of
https://github.com/saltstack/salt.git
synced 2025-04-17 10:10:20 +00:00
Fix race condition in ps.top
When a process ends between the start of the function and the end, a psutil.NoSuchProcess exception will be raised if you try to invoke member functions of a psutil.Process object for that PID.
This commit is contained in:
parent
92fc17115b
commit
0db615b40b
1 changed files with 10 additions and 4 deletions
|
@ -178,10 +178,16 @@ def top(num_processes=5, interval=3):
|
|||
"cpu": {},
|
||||
"mem": {},
|
||||
}
|
||||
for key, value in six.iteritems(process.cpu_times()._asdict()):
|
||||
info["cpu"][key] = value
|
||||
for key, value in six.iteritems(process.memory_info()._asdict()):
|
||||
info["mem"][key] = value
|
||||
try:
|
||||
for key, value in six.iteritems(process.cpu_times()._asdict()):
|
||||
info["cpu"][key] = value
|
||||
for key, value in six.iteritems(process.memory_info()._asdict()):
|
||||
info["mem"][key] = value
|
||||
except psutil.NoSuchProcess:
|
||||
# Process ended since psutil.pids() was run earlier in this
|
||||
# function. Ignore this process and do not include this process in
|
||||
# the return data.
|
||||
continue
|
||||
result.append(info)
|
||||
|
||||
return result
|
||||
|
|
Loading…
Add table
Reference in a new issue