On 2007-06-29, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote: > I want to end the execution of the command when the user closes my > application. > > Right now I'm using an object my_child of type subprocess.Popen to > execute the command, inside a thread with an infinite loop where we > constantly ask for its output. > > To end the program when the user closes the application, I send a > SIGTERM to the process with pid my_child.pid using os.kill. But I also > have to send a SIGTERM to my_child.pid + 1 because my_child.pid is the
Maybe that holds for a system recently started and mostly idle, but it need not be the case. If you have more users active, and each user is forking processes, (or one user is forking several processes concurrently), the order of assigning process IDs is not defined. For systems that run for a longer time, unused process IDs are re-used, skipping over process IDs that are still living. > pid of /bin/sh -c which is the one which calls the command, because > when I try to run Popen with shell=False, it sends an exception and > says the file or directory doesn't exist. The shell performs a search over PATH to find the command executable (ie it maps 'ls' to '/bin/ls'). If you provide a full path to the command you are starting, you don't need the shell. > Anyone knows of a better way to close the command than using a > SIGTERM? I just can't help myself thinking this is an ugly dirty hack. In principle, you should only kill your own child processes, your child process should handle its own childs (your grant child processes). SIGTERM is one way. Another solution often adopted is to close the stdin of the child. This notifies the child that no more data will arrive, and many commands react on that message by terminating. Try running command < /dev/null /dev/null is the empty input stream. Last but not least, many commands do something special when sent -HUP. Look in the manual page of the command for clean ways to close the command down. Albert -- http://mail.python.org/mailman/listinfo/python-list