473,394 Members | 1,965 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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

I'm writing a program which has to execute a command, get its output
and show it on a treeview.
This command runs for a very long time.
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
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.

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.

Jun 29 '07 #1
2 9117
A.T.Hofkamp <ha*@se-162.se.wtb.tue.nlwrote:
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.
That is a good idea.

You could also make the parent a process group leader and kill the
process group. You'll get the kill signal too which you'll need to
ignore. That is how the shell keeps track of things IIRC.

Read man setpgid, getpgid should be helful. There are equivalent
commands in os, ie os.setpgid and os.getpgid.

--
Nick Craig-Wood <ni**@craig-wood.com-- http://www.craig-wood.com/nick
Jul 1 '07 #2
On Jun 29, 6:36 pm, revenan...@hotmail.com wrote:
I'm writing a program which has to execute a command, get its output
and show it on a treeview.
This command runs for a very long time.
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
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.

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.
As nick pointed out use process group's .
I use the "preexec_fn" keyword argument to Popen and "os.setsid()"
call's side effect
to make process groups and then os.killpg() to send the signal to
process groups.

child = Popen( cmd , preexec_fn = os.setsid )
os.killpg( child.pid,signal.SIGINT)

Regards
jitu


Jul 6 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Darren Dale | last post by:
I'm a developer on the matplotlib project, and I am having trouble with the subprocess module on windows (Python 2.4.2 on winXP). No trouble to report with linux. I need to use _subprocess instead...
3
by: madpython | last post by:
playing with subprocess.Popen on Windows I stumbled into the following problem: Python 2.4.3 (#69, Mar 29 2006, 17:35:34) IDLE 1.1.3 >>> import subprocess >>>...
7
by: skunkwerk | last post by:
Hi, i'm trying to call subprocess.popen on the 'rename' function in linux. When I run the command from the shell, like so: rename -vn 's/\.htm$/\.html/' *.htm it works fine... however when I...
8
by: clyfish | last post by:
In cmd, I can use find like this. C:\>netstat -an | find "445" TCP 0.0.0.0:445 0.0.0.0:0 LISTENING UDP 0.0.0.0:445 *:* C:\> And os.system is OK....
0
by: srinivasan srinivas | last post by:
Hi, My requirement is i have to execute a python script on a remote machine as a subprocess from a python script and to get the subprocess pid of the process running the script. Is there anyway to...
0
by: srinivasan srinivas | last post by:
This is ok. Is there any other way to find it out? Thanks, Srini ----- Original Message ---- From: Gerhard Häring <gh@ghaering.de> To: python-list@python.org Sent: Friday, 20 June, 2008...
25
by: Jeremy Banks | last post by:
Hi. I wondered if anyone knew the rationale behind the naming of the Popen class in the subprocess module. Popen sounds like the a suitable name for a function that created a subprocess, but the...
1
by: Mark Shewfelt | last post by:
Hello, I am attempting to use Popen() in a Windows service. I have a small Win32 .exe that I normally run through the os.popen2() function. I've written a class to work with the input and output...
3
by: ckkart | last post by:
Hi, on XP when starting a certain external program (plain C calculation program which communicates via stdout/fs) from python 2.5 using subprocess.Popen the external program crashes. It does not...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.