read stdout/stderr without blocking

Adriaan Renting renting at astron.nl
Mon Sep 12 04:49:04 EDT 2005


Check out the select module, for an example on how to use it:
pexpect.sourceforge.net


 
 
>>>Jacek Pop*awski <jpopl at interia.pl> 09/12/05 10:07 am >>> 
Popen from subprocess module gives me access to stdout, so I can read 
it. Problem is, that I don't know how much data is available... How can 
I read it without blocking my program? 
 
example: 
-------------------------------------------------------------------- 
import subprocess 
import time 
 
command="ls -l -R /" 
 
p=subprocess.Popen(command,shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE) 
 
while (p.poll()==None): 
        print "." 
        r=p.stdout.read() 
-------------------------------------------------------------------- 
 
when you comment out read() - you will notice that loop is working, with 
read() loop is blocked 
Of course I don't need to read() inside loop, but... if output is very 
long (like from "make") and I don't read from stdout - command will 
block itself! I tried to increase bufsize, but it didn't help. 
 
Is there a way to read only available data from stdout/stderr? 
Is there a way to not block Popen command without reading stdout/stderr? 
-- 
http://mail.python.org/mailman/listinfo/python-list 




More information about the Python-list mailing list