You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
import collections
import sys
import psutil
def print_tree(parent, tree, indent=''):
try:
name = psutil.Process(parent).name()
except psutil.Error:
name = "?"
print(parent, name)
if parent not in tree:
return
children = tree[parent][:-1]
for child in children:
sys.stdout.write(indent + "|- ")
print_tree(child, tree, indent + "| ")
child = tree[parent][-1]
sys.stdout.write(indent + "`_ ")
print_tree(child, tree, indent + " ")
def main():
# construct a dict where 'values' are all the processes
# having 'key' as their parent
tree = collections.defaultdict(list)
for p in psutil.process_iter():
try:
tree[p.ppid()].append(p.pid)
except (psutil.NoSuchProcess, psutil.ZombieProcess):
pass
# on systems supporting PID 0, PID 0's parent is usually 0
if 0 in tree and 0 in tree[0]:
tree[0].remove(0)
print_tree(min(tree), tree)
if __name__ == '__main__':
main()
and all I've got on windows7 was this little subset:
0 System Idle Process
`_ 4 System
`_ 600 smss.exe
What's the reason of it? print(len(sorted(psutil.pids()))) is giving me 177... Ideally I'd like to get the same output than the one provided by process explorer and displaying the full tree.
Also, it's kinda unrelated to this thread topic but I'd like to know whether it'd be possible to mimick the behaviour of handle using psutil... I'm trying to solve this problem and I think psutil can be really helpful here.
Thanks in advance.
The text was updated successfully, but these errors were encountered:
Haha :'D , well, considering it's the script you've written and tested (probably only on unix?), psutil is the library you've created & maintain for quite a while... there was a good chance you'd know about this issue straightaway, wasn't?
Anyway, guess you're right... I'll get my hands dirty and see why your script doesn't work properly ;) . About the other issue/question in my above thread, any idea?
Found it, it seems your code is assuming you'll get a process tree while on windows you'll get a forest (at least the way you're creating the tree).
One easy workaround would be to create an invisible root item so it becomes the parent of the root indices, that way your algorithm will work also on windows.
I've tried running pstree.py:
and all I've got on windows7 was this little subset:
What's the reason of it?
print(len(sorted(psutil.pids())))
is giving me 177... Ideally I'd like to get the same output than the one provided by process explorer and displaying the full tree.Also, it's kinda unrelated to this thread topic but I'd like to know whether it'd be possible to mimick the behaviour of handle using psutil... I'm trying to solve this problem and I think psutil can be really helpful here.
Thanks in advance.
The text was updated successfully, but these errors were encountered: