Finding child pids

Tom Chance tomchance at gmx.net
Sun Mar 16 17:56:13 EST 2003


Hello again,

I've got some code that opens a pipe using popen2.Popen3, and reads data
from the pipe. I need to be able to kill the process at any time, and at
present I can do this:

---
# To open the pipe
self.pipe = popen2.Popen3(command)

# To kill it (seperate section of the program)
self.pid = self.pipe.pid + 1
os.kill(self.pid, 9)
os.waitpid(self.pipe.pid, os.WNOHANG)
---

The problem here is in the line where I have to add 1 to the pid of the
child process. When I open the pipe, python opens the command with sh, and
so the first child process is sh, and sh then makes the command its child
process. SO... if I kill the pipe (sh), it just then leaves the command to
detach and work on its own. So I have to increment the pid by one to kill
the command instead. But this seems like a nasty hack to me.

How can I get the pid of the command, and not just sh?

Tom




More information about the Python-list mailing list