[Tutor] subprocess and signals

Eric Brunson brunson at brunson.com
Tue Aug 7 20:42:06 CEST 2007


Noufal Ibrahim wrote:
> 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
>   

I'm not an expert on the subprocess module, but I've used it a bit and 
I'm referring to the docs as I write this.

> 1. I don't see any output (from my 'build') on screen when I run my 
> wrapper.
>   

Why do you think you should?  You've asked subprocess to pipe its output 
you your parent process, I believe you would need to use the 
communicate() method to get the output generated by the subprocess.


> 2. When I send a ^C to the wrapper, I don't see the output from the 
> build script.
>   

Fix #1 first and then this should fall into place.

> 3. I sometimes get a pipe error and the whole wrapper dies leaving the 
> other program running.
>   

Exact error messages help a lot.  :-)

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



More information about the Tutor mailing list