Terminating a subprocess '.exe'

E

Ernesto

I launch a Windows executable and wish to close it from Python. The
code is below. Normally, my program waits for rib.exe to finish, but
I'd like to be able to close it from python if possible. (I suppose if
I was going to do this, I wouldn't use .wait() ) Any ideas ?

# Launch a program without launching a whole new cmd prompt
def launchWithoutConsole(command, args):
"""Launches 'command' windowless and waits until finished"""
startupinfo = subprocess.STARTUPINFO()
startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
return subprocess.Popen([command] + args,
startupinfo=startupinfo).wait()

ribHandle = launchWithoutConsole("rib.exe",["recovery"])

# Is there a way to close "rib.exe" using ribHandle ?
 
F

Fredrik Lundh

Ernesto said:
I launch a Windows executable and wish to close it from Python. The
code is below. Normally, my program waits for rib.exe to finish, but
I'd like to be able to close it from python if possible. (I suppose if
I was going to do this, I wouldn't use .wait() ) Any ideas ?

# Launch a program without launching a whole new cmd prompt
def launchWithoutConsole(command, args):
"""Launches 'command' windowless and waits until finished"""
startupinfo = subprocess.STARTUPINFO()
startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
return subprocess.Popen([command] + args,
startupinfo=startupinfo).wait()

ribHandle = launchWithoutConsole("rib.exe",["recovery"])

# Is there a way to close "rib.exe" using ribHandle ?

for 2.4 and earlier, see:

http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/347462

for 2.5 (upcoming) and later, you can use either the ctypes method
or the _subprocess.TerminateProcess() call.

</F>
 
E

Ernesto

Fredrik said:
Ernesto said:
I launch a Windows executable and wish to close it from Python. The
code is below. Normally, my program waits for rib.exe to finish, but
I'd like to be able to close it from python if possible. (I suppose if
I was going to do this, I wouldn't use .wait() ) Any ideas ?

# Launch a program without launching a whole new cmd prompt
def launchWithoutConsole(command, args):
"""Launches 'command' windowless and waits until finished"""
startupinfo = subprocess.STARTUPINFO()
startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
return subprocess.Popen([command] + args,
startupinfo=startupinfo).wait()

ribHandle = launchWithoutConsole("rib.exe",["recovery"])

# Is there a way to close "rib.exe" using ribHandle ?

for 2.4 and earlier, see:

http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/347462

for 2.5 (upcoming) and later, you can use either the ctypes method
or the _subprocess.TerminateProcess() call.

</F>

Works EXACTLY the way I want it to. Thanks Fredrik !
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,792
Messages
2,569,639
Members
45,353
Latest member
RogerDoger

Latest Threads

Top