I have two files first of which is fizz
#!/usr/bin/python
import time
print 'started'
time.sleep(3)
print 'ended'
next of which is bar
#!/usr/bin/python
import sys
for line in sys.stdin:
print line
When I run the command ./fizz | ./bar I expect it to print started then wait 3 seconds and print ended, but what really happends is that it prints started and ended at the same time after 3 seconds. Is there a way to get my desired behavior? Thanks
Good question. It is a little harder than it should.
The issue is indeed in
bar, specificallysys.stdinbeing buffered. I tried openingsys.stdinwith a smaller buffer size and usingpython -ubut that didn’t work. The manpage has this to say:In the end this is what worked for me: