Re: How to close a program I execute with subprocess.Popen?



On 2007-06-29, revenant81@xxxxxxxxxxx <revenant81@xxxxxxxxxxx> 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
.



Relevant Pages

  • Re: Getting current time in milliseconds
    ... That is impossible because child processes cannot alter variables of the ... See "perldoc -q env" for a possible workaround. ... The workaround suggested in "perldoc -q env" requires the Perl script ... to generate a shell command to be evaluated, ...
    (comp.lang.perl.misc)
  • Re: Why Does This Script Rin So Slow?
    ... The kernel has to forkfor each command, ... In looping over the directory of 2+ million entries, the ridiculously slow script is creating 2+ million child processes (each of which does ... You may be much better off using awk, ... which uses 3 processes, the first lists the files, the second creates a command to rename the file to lower case and the third then executes the created commands. ...
    (alt.linux)
  • How to close a program I execute with subprocess.Popen?
    ... I'm writing a program which has to execute a command, ... This command runs for a very long time. ... SIGTERM to the process with pid my_child.pid using os.kill. ...
    (comp.lang.python)
  • Re: SETing environmental variables permantly
    ... | That's not via the command line, ... Anything you would do would be a daughter process and all child processes would inherit the ... Environmental variables but the parent process won't. ...
    (microsoft.public.windowsxp.general)
  • Killing child processes on W2K
    ... I have to run a command that normally hangs on the first attempt. ... script that will spawn a child processes to execute the command (I'm ... expecting them to hang) then have the parent come back and kill all ... The script I've writtten ...
    (comp.lang.perl.misc)