Closed GoogleCodeExporter closed 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:
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:
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
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
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
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
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
can I close this issue somehow?
Original comment by dolfandr...@gmail.com
on 19 Jan 2012 at 10:20
Closing this out.
Original comment by g.rodola
on 19 Jan 2012 at 1:37
Original issue reported on code.google.com by
dolfandr...@gmail.com
on 19 Jan 2012 at 8:47Attachments: