[Tutor] subprocess and signals

Noufal Ibrahim noufal at airtelbroadband.in
Tue Aug 7 20:28:08 CEST 2007


Hello everyone,
   I've come across a situation which is somewhat confusing to me.
   I googled for some details and came across another email thread on 
this very list but couldn't really glean a solution out of it.

   I have a program (a compiled binary) for which I need to write a 
wrapper (in python). The wrapper will construct some sane command line 
defaults for this binary and then execute it while storing some 
statistics like who launched it, where and when.

   Now this program will continuously print output (it's a parallel 
version of make). It it receives a SIGINT (via a Ctrl-C), it will print 
some statistics and go on with the build. If it receives a Ctrl-\ 
(SIGQUIT I think), it will terminate. I want my wrapper to be able to 
read the output of this program and print it while allowing these two 
signals to be passed to it.

   My wrapper (let's call it wrapper.py) has something like this

------------------------------------------------------------------
def createSignalDelegator(p):
     def sighandler(signal,frame,pmake = p):
         os.kill(pmake.pid,signal)
     return sighandler

pmake = subprocess.Popen(pmake_cmd, bufsize = 1, stdout = 
subprocess.PIPE, stderr = subprocess.STDOUT)

signal.signal(signal.SIGINT,createSignalDelegator(pmake))
signal.signal(signal.SIGQUIT,createSignalDelegator(pmake))
for op in pmake.stdout:
    print "-- %s"%str(op).strip()
------------------------------------------------------------------

I've substituted my actual binary with a simple python script that 
continuously prints a string and which traps sigint and sigquit to 
appear like the binary. It seems to be receiving the signals fine (since 
I log it's activity into a separate file) but the problems I get are like so

1. I don't see any output (from my 'build') on screen when I run my 
wrapper.
2. When I send a ^C to the wrapper, I don't see the output from the 
build script.
3. I sometimes get a pipe error and the whole wrapper dies leaving the 
other program running.

I'd appreciate any insights into the problem. I'm not sure where to 
start looking.

Thanks.


-- 
~noufal


More information about the Tutor mailing list