Reputation: 33225
In speaker.py
, I use print
to output text to STDOUT
:
import time
while True:
time.sleep(1)
print("hello")
And in listener.py
, I use input
to read from STDIN
:
while True:
line = input()
if not line:
break
print(line)
I'm trying to connect these two scripts with a pipe:
python speaker.py | python listener.py
But listner.py
output nothing.
What's wrong?
Upvotes: 4
Views: 195
Reputation: 217
An alternative to re-opening stdout as Andrea mentioned is to start up Python in unbuffered mode with the -u option:
python -u speaker.py | python -u listener.py
Upvotes: 5
Reputation: 189387
Nothing is wrong per se, but you bumped into buffering. Take out the sleep
and you should see output pretty much immediately.
http://mywiki.wooledge.org/BashFAQ/009 is nominally a Bash question, but applies to any Unix-type I/O, and explains the issues thoroughly.
Upvotes: 5