473,782 Members | 2,623 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to close a program I execute with subprocess.Pope n?

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.Pope n 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 9157
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...@hotm ail.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.Pope n 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,signa l.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
5507
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 of pywin32, but my trouble exists with either option: import subprocess process = subprocess.Popen(, stderr=subprocess.STDOUT, stdout=subprocess.PIPE) stat = process.wait() print process.stdout.read()
3
12753
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 >>> p1=subprocess.Popen("c:\\asd.bat") #works OK >>> p2=subprocess.Popen("c:\\asd.bat",stdout=subprocess.PIPE)
7
4934
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 try to do it in python like so: p = subprocess.Popen(,stdout=subprocess.PIPE,stderr=subprocess.PIPE) print p.communicate()
8
4215
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. TCP 0.0.0.0:445 0.0.0.0:0 LISTENING
0
1129
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 do that?? I have used subprocess.popen() method to do that. I have done as following: executable = '/usr/bin/rsh' args = pid = subprocess.popen(args) It returned the pid of rsh. But i am interested in the pid of the process running the script....
0
2055
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 10:03:30 PM Subject: Re: Execute a script on a remote machine
25
2794
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 object itself is a subprocess, not a "popen". It seems that it would be more accurate to just name the class Subprocess, can anyone explain why this is not the case? Thank you.
1
8679
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 parameters that are passed and captured from this exe. When I use the class outside of a service using either subprocess.Popen or os.popen2 work just fine. When I use this class inside a Windows service it doesn't work. It doesn't crash the...
3
4736
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 if started directly from the XP command prompt. This is not a purely python problems since the crash occurs as well if started e.g. from a msys bash shell. The only things I find worth mentioning from the windows debugging message is that...
0
9474
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10308
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10076
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7486
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5375
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5507
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4040
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3633
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2870
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.