[Tutor] subprocess and signals

Noufal Ibrahim noufal at airtelbroadband.in
Tue Aug 7 21:23:09 CEST 2007

Eric Brunson wrote:
> Noufal Ibrahim wrote:
>> 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'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.

If I read from the stdout attribute of the Popen object, I should get 
the output of the pipe. Shouldn't I? That's what I'm trying to do. I 
think the code above is broken though so I've changed the last two lines 
of the snippet to look like this

line = pmake.stdout.readline()
while line:
     print "-- %s"%str(line).strip()
     line = pmake.stdout.readline()

That sounds more sensible. The docs for communicate warn against using 
it when the data size is large and that's the case here. I did however 
try it like so
(stdout,dummy) = pmake.communicate()
line = stdout.readline()
while line:
    print "-- %s"%str(line).strip()
    line = stdout.readline()

and I get similar behaviour.

>> 3. I sometimes get a pipe error and the whole wrapper dies leaving the 
>> other program running.
> Exact error messages help a lot.  :-)

Yeah I know. I didn't have access to the machine where I got the 
original error so I thought I'd slur around there a bit. :)

But I got it now (this is with the original snippet).

Traceback (most recent call last):
   File "./build_wrapper.py", line 246, in <module>
   File "./build_wrapper.py", line 231, in main
   File "./build_wrapper.py", line 209, in run_pmake
     line = pmake.stdout.readline()
IOError: [Errno 4] Interrupted system call

This is with python2.4 (if that's useful). Also, the output of the 
actual program will be a bit slow (since it's doing compiles and stuff). 
Will some intermediate buffering create any trouble?



More information about the Tutor mailing list