467,212 Members | 1,157 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,212 developers. It's quick & easy.

read stdout/stderr without blocking

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=subpr ocess.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?
Sep 12 '05 #1
  • viewed: 2610
Share:
1 Reply
Only solution which works for now is to redirect stderr to stdout, and
read stdout on thread.
Code without thread or with read() or read(n) (when n>1) can block.
Code with select() and read(1) works, but it is very slow.
Sep 13 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Tsai Li Ming | last post: by
31 posts views Thread by Randy Yates | last post: by
9 posts views Thread by Adi | last post: by
2 posts views Thread by Guillaume Dargaud | last post: by
reply views Thread by Olivier Lefevre | last post: by
13 posts views Thread by rohit | last post: by
63 posts views Thread by Bill Cunningham | last post: by
5 posts views Thread by Thomas Christensen | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.