cw-oldrepos / psutil

Automatically exported from code.google.com/p/psutil
Other
0 stars 0 forks source link

Process tree object, including code/fix #241

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
I just started using the psutil library and needed a tree object where each 
process also contains references to it's parent and child processes, and a way 
to search the tree for processes by pid or by process name.

I made an addition therefore to psutil.__ini__.py which contains a ProcessNode 
object, which is mainly a Process object with references to parent and 
children, and a ProcessTree object, which constructs the tree, and enables 
searching the tree for processes by pid and by name. 

I attached a diff for this functionality.
I hope it is useful and might be included in the official code. 
Cheers,

Dolf.

Original issue reported on code.google.com by dolfandr...@gmail.com on 19 Jan 2012 at 8:47

Attachments:

GoogleCodeExporter commented 9 years ago
Sorry, there were two minor errors in the diff at get_process_by_name, an error 
in the docstring, and and error in the code. A new diff, against the original 
__init_.py with those errors fixed, is attached.

Original comment by dolfandr...@gmail.com on 19 Jan 2012 at 8:52

Attachments:

GoogleCodeExporter commented 9 years ago
And yet another small change. I decided (while using my new code in another 
app) that it would be better to rename get_process_by_name to 
get_processes_by_name (plural) and to always return a list, even just an empty 
one when now processes were found. This way you can always iterate over the 
results, knowing it will return a list.

Original comment by dolfandr...@gmail.com on 19 Jan 2012 at 9:06

Attachments:

GoogleCodeExporter commented 9 years ago
Mmm this looks like a complex solution for a simple problem to me.
If I understand your patch correctly ProcessTree class offers two facilities: 
get process by name and by pid.
Both functionnalities can be easily written pretty easily already. 

def proc_by_name(name):
    for proc in psutil.process_iter():
        if name in proc.name:
            yield proc

Process by pid is actually just a simple wrapper around Process class:

def proc_by_pid(pid):
    try:
        psutil.Process(pid):
    except psutil.NoSuchProcess:
        pass  # or let exception raise 

Original comment by g.rodola on 19 Jan 2012 at 9:30

GoogleCodeExporter commented 9 years ago
The main functionality is not searching the tree, the main functionality is the 
ProcessNode wrapper, which provides a reference to the parent process object, 
and the child process object. So I can find a process by name on the Tree, and 
then loop over it's child processes or instance, or check the parent process. 
The latter would be easy by using Process(Process.ppid), but finding the child 
processes is another matter. 

Original comment by dolfandr...@gmail.com on 19 Jan 2012 at 9:42

GoogleCodeExporter commented 9 years ago
An example would be this:

ptree=ProcessTree()
for proc in ptree.get_processes_by_name('terminal'):
   print [p.name for p in proc.children]

Original comment by dolfandr...@gmail.com on 19 Jan 2012 at 9:44

GoogleCodeExporter commented 9 years ago
Please provide a use case and the corresponding code sample using 
ProcessTree/ProcessNode.

> So I can find a process by name on the Tree, and then loop 
> over it's child processes or instance, or check the parent process.

loop over children:

>>> for p in psutil.Process(pid).get_children():
...     # do stuff
...
>>>

check parent process:

>>> psutil.Process(3).parent
<psutil.Process(pid=2, name='kthreadd') at 18794576>
>>>
>>> # ...in case there's no parent we'll get None
>>> psutil.Process(3123).parent
>>>

Original comment by g.rodola on 19 Jan 2012 at 9:53

GoogleCodeExporter commented 9 years ago
darn, I must have missed the get_children method in the docs :) That was a 
waste of time. There is then one thing that I can still add 
(get_lowest_descendants), and I will add a diff for that in a new issue. Thanks 
for bearing with me :D

Original comment by dolfandr...@gmail.com on 19 Jan 2012 at 10:20

GoogleCodeExporter commented 9 years ago
can I close this issue somehow?

Original comment by dolfandr...@gmail.com on 19 Jan 2012 at 10:20

GoogleCodeExporter commented 9 years ago
Closing this out.

Original comment by g.rodola on 19 Jan 2012 at 1:37