Non-blocking read on a stream in python.

Posted by Mathieu Pagé on Stack Overflow See other posts from Stack Overflow or by Mathieu Pagé
Published on 2008-12-17T17:56:34Z Indexed on 2010/03/18 8:41 UTC
Read the original article Hit count: 737

Filed under:
|
|
|

Hi,

I'm using the subprocess module to start a subprocess and connect to it's output stream (stdout). I want to be able to execute non-blocking reads on its stdout. Is there a way to make .readline non-bloking or to check if there is data on the stream before I invoke .readline? I'd like this to be portable or at least work under Windows and Linux.

here is how I do it for now (It's blocking on the .readline if no data is avaible):

p = subprocess.Popen('myprogram.exe', stdout = subprocess.PIPE)
str = p.stdout.readline()

Thanks for your help.

© Stack Overflow or respective owner

Related posts about python

Related posts about subprocess