Check for num_processes based on length of return list

This commit is contained in:
Erik Johnson 2020-04-28 12:35:37 -05:00 committed by Daniel Wozniak
parent c208101d66
commit 05e4822199

View file

@ -163,11 +163,7 @@ def top(num_processes=5, interval=3):
diff = now - start
usage.add((diff, process))
for idx, (diff, process) in enumerate(
sorted(usage, key=lambda x: x[0], reverse=True)
):
if num_processes and idx >= num_processes:
break
for diff, process in sorted(usage, key=lambda x: x[0], reverse=True):
info = {
"cmd": _get_proc_cmdline(process) or _get_proc_name(process),
"user": _get_proc_username(process),
@ -187,8 +183,13 @@ def top(num_processes=5, interval=3):
# function. Ignore this process and do not include this process in
# the return data.
continue
result.append(info)
# Stop gathering process info since we've reached the desired number
if len(result) >= num_processes:
break
return result