Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pstree not displaying the full tree on windows #1327

Open
brupelo opened this issue Aug 17, 2018 · 4 comments
Open

pstree not displaying the full tree on windows #1327

brupelo opened this issue Aug 17, 2018 · 4 comments
Labels

Comments

@brupelo
Copy link

brupelo commented Aug 17, 2018

I've tried running pstree.py:

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.

@giampaolo
Copy link
Owner

What's the reason of it?

How can I know? :)
Either provide more information or try to debug pstree.py yourself.

@brupelo
Copy link
Author

brupelo commented Aug 17, 2018

How can I know? :)

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?

@brupelo
Copy link
Author

brupelo commented Aug 17, 2018

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.

@brupelo brupelo closed this as completed Aug 21, 2018
@giampaolo
Copy link
Owner

Wow. It suppose I never tested it on Windows then. Can you provide a PR?

@giampaolo giampaolo reopened this Aug 21, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants