kiranvizru / psutil

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

get a process by name and get a processes' descendants #242

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Here is a diff for two new additions to psutil.
One is psutil.proc_by_name, which g.rodola actually wrote in issue 241. I just 
included it here in the diff, and changed it from returning an iterator to a 
list.

The other is a get_lowest_descendants method on the Process object, which 
fetches all descendants from a process, that don't have child processes 
themselves.

The use case that I have for this method is that I want to see what I am 
currently doing in my terminal application. So my terminal will have a couple 
of bash child processes (one for each tab), and each of those has child 
processes as well, some being an ssh command into another machine, some being a 
python interpreter in a certain folder, or a vi instance on a certain file, etc.

With the get_lowest_descendants method I can easily fetch those descendants and 
what they are doing.

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

GoogleCodeExporter commented 9 years ago
and now with the diff attached :)

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

Attachments:

GoogleCodeExporter commented 9 years ago
> patch in attachment adds a get_lowest_descendants()
> method on the Process object, which fetches all 
> descendants from a process, that don't have 
> child processes themselves.

Mmm interesting.
I think I don't have a clear mind about this yet.
At first it seems kind of useful, on the other hand I'm not sure about its 
reliability.
Imagine this:

process A creates process B which creates process C

In normal circumstances your method would return A, B and C, but if B gets 
terminated in meantime you're gonna lose the reference to C and get only A in 
return.

Other than that, I think this would be better served by adding an extra 
argument to get_children() method:

>>> get_children(recursive=True)

...and make it return only B and C but not itself (A).

Also, it seems natural to make get_children() return an iterator rather than a 
list.
The same should be done for get_open_files(), get_connections() and 
get_threads() (I'll file a new ticket for that).

Jay, what do you think?

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

GoogleCodeExporter commented 9 years ago
In your example where process A creates process B which creates process C, 
the code would only return process C, not B, since B has children.

Another option for my use case would then be 
>>> filter(lambda x: len(x.children)==0,get_children(recursive=True))

Original comment by dolfandr...@gmail.com on 19 Jan 2012 at 2:07

GoogleCodeExporter commented 9 years ago
This is now implemented in r1282.
Please look at the docstring and let me know if with this you're able to cover 
your use case.

Original comment by g.rodola on 7 Apr 2012 at 3:38

GoogleCodeExporter commented 9 years ago
Change recursion algorithm as r1283.

Original comment by g.rodola on 8 Apr 2012 at 12:37

GoogleCodeExporter commented 9 years ago

Original comment by g.rodola on 12 Apr 2012 at 6:15

GoogleCodeExporter commented 9 years ago
0.5.0 is finally out. Closing out as fixed.

Original comment by g.rodola on 27 Jun 2012 at 6:54

GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
Updated csets after the SVN -> Mercurial migration:
r1282 == revision 19893ece7eff
r1283 == revision 0e9fabea61e2

Original comment by g.rodola on 2 Mar 2013 at 12:06