5

I'm very new to programming so I apologize in advance if my question is too silly.

#!/usr/bin/python2.6  
import subprocess, time  
p=subprocess.Popen(['cat'], stdin=subprocess.PIPE, stdout=subprocess.PIPE)  
for i in 'abcd':  
    p.stdin.write(str.encode(i+'\n'))  
    output=p.stdout.readline()  
    print(output)  
    time.sleep(1)

Executing this code in Python 2.6 prints letters a, b, c, d , each line of output appears after a second. This is expected behavior. But in Python 3.1 execution is blocked at line output=p.stdout.readline(). How to correct this for Python 3.1?

4
  • what exactly happens when the execution is blocked? Do you have an error you could show? Commented Dec 20, 2009 at 19:37
  • not errors, only prompts for input; this code was executed in terminal window Commented Dec 20, 2009 at 19:47
  • 5
    At a guess it might be differences in buffering; does something change if you add a call to p.stdin.flush after the write? Commented Dec 20, 2009 at 19:52
  • 1
    Thanks a lot, you answer my question; adding p.stdin.flush() solve my problem Commented Dec 20, 2009 at 20:00

1 Answer 1

3

Appears to be a difference in buffering. Adding a p.stdin.flush() call solved the problem. (See the comments above).

Community wiki as I deserve no credits for this answer, but some answer needs to be marked accepted.

[@Geo Pop: Please "accept" this question, as it apparently is correct.]

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.