Python: Streaming Input with Subprocesses

Posted by beary605 on Stack Overflow See other posts from Stack Overflow or by beary605
Published on 2012-06-11T04:24:25Z Indexed on 2012/06/11 4:40 UTC
Read the original article Hit count: 175

Filed under:
|
|

Since input and raw_input() stop the program from running anymore, I want to use a subprocess to run this program...

while True: print raw_input()

and get its output.

This is what I have as my reading program:

import subprocess
process = subprocess.Popen('python subinput.py', stdout=subprocess.PIPE, stderr=subprocess.PIPE) 
while True:
    output=process.stdout.read(12)
    if output=='' and process.poll()!=None:
        break
    if output!='':
        sys.stdout.write(output)
        sys.stdout.flush()

When I run this, the subprocess exits almost as fast as it started. How can I fix this?

© Stack Overflow or respective owner

Related posts about python

Related posts about input