Killing children

Peter Hansen peter at engcorp.com
Fri Aug 6 13:47:59 EDT 2004


Laura Conrad wrote:

> I'm writing an application that has to spawn some processes and then
> kill them later.  It doesn't need to talk or listen to them while
> they're running, but there are stop and start buttons and the stop
> button should stop everything that gets started by the start button.
> 
> There are lots of ways to do this on Linux, but this project has to
> run under cygwin.  So far, the only thing I've gotten to work at all
> under cygwin is doing an 'os.system' to start the process and then
> parsing the "ps" output and using os.kill on the processes that ps
> finds.  This is unsatisfactory in a number of ways.  
> 
> I have been unable to get os.spawnl to start the processes correctly.
> I was using pexpect, which worked fine with test programs, but not
> with the actual processes that need to be spawned.  I did a little bit
> of playing with the "process" module but didn't get it working right.
> 
> Does anyone have a better idea?  

This has been successful in some cases:

if sys.platform == 'win32':
     def _kill(pid, sig):
         '''pid is actually handle, but that's what os.spawn* returns on 
Win32'''
         import win32api
         win32api.TerminateProcess(pid, 0)  # ignore sig, 0 is return 
code for process
     import os
     os.kill = _kill
     del _kill


It requires win32api, and I don't know how that all will work with
cygwin (which I don't use).  Basically it lets you use os.kill()
in a platform-independent fashion (provided you care only about
Linux and Windows NT/XP/2K ;-).

-Peter



More information about the Python-list mailing list