Why does this code behave differently in Python3.1 than in Python2.6?

I am very new to programming, so I apologize in advance if my question is too stupid.

#!/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)

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

+5
source share
1 answer

There is a difference in buffering. Adding a call p.stdin.flush()solved the problem. (See comments above).

wiki, , .

[@Geo Pop: , "" , , -, .]

+3

All Articles