Reading from sys.stdin reads the whole file in

Steven D'Aprano steve at pearwood.info
Wed Aug 27 01:19:05 EDT 2014


I'm trying to read from stdin. Here I simulate a process that slowly 
outputs data to stdout:

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

print "Hello..."
time.sleep(10)
print "World!"
time.sleep(10)
print "Goodbye!"


and another process that reads from stdin:

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

for line in sys.stdin:
	print time.ctime(), line



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:


steve at runes:~$ python out.py | python slurp.py 
Wed Aug 27 15:13:44 2014 Hello...

Wed Aug 27 15:13:44 2014 World!

Wed Aug 27 15:13:44 2014 Goodbye!



(Note how the time stamps are all together, instead of ten seconds apart.)


Why is this happening?

How can I read from sys.stdin "on the fly", so to speak, without waiting 
for the first process to end?

Is there established terminology for talking about this sort of thing?



-- 
Steven



More information about the Python-list mailing list