Reading from sys.stdin reads the whole file in

Steven D'Aprano steve at pearwood.info
Wed Aug 27 02:37:31 EDT 2014


On Wed, 27 Aug 2014 08:29:20 +0300, Marko Rauhamaa wrote:

> Steven D'Aprano <steve at pearwood.info>:
> 
>> When I pipe one to the other, I expect each line to be printed as they
>> arrive, but instead they all queue up and happen at once:
> 
> Try flushing after each print.

Doesn't help.

Here is an update that may make the problem more clear:

steve at runes:~$ cat out.py
import time
import sys

print "Time of output:", time.ctime()
sys.stdout.flush()
time.sleep(10)
print "Time of output:", time.ctime()
sys.stdout.flush()
time.sleep(10)
print "Time of output:", time.ctime()

steve at runes:~$ cat slurp.py 
import sys
import time

for line in sys.stdin:
	print "Time of input:", time.ctime(), line
        sys.stdin.flush()
        sys.stdout.flush()



And the results:

steve at runes:~$ python out.py | python slurp.py 
Time of input: Wed Aug 27 16:35:48 2014 Time of output: Wed Aug 27 16:35:28 2014

Time of input: Wed Aug 27 16:35:48 2014 Time of output: Wed Aug 27 16:35:38 2014

Time of input: Wed Aug 27 16:35:48 2014 Time of output: Wed Aug 27 16:35:48 2014


-- 
Steven



More information about the Python-list mailing list