2

I have a script that writes to stdout like this:

# Send.py
import time

while(1):
    print "hello"
    time.sleep(0.1)

Now I have another script that needs to read it as such:

# Listen.py
while(1) :

    print "reading.."
    data = sys.stdin.read()
    print data

python Send.py | python listen.py

Unfortunately, this just stops on reading and nothing is every printed. Why is that?

falsetru
  • 357,413
  • 63
  • 732
  • 636
ovfstack
  • 241
  • 1
  • 3
  • 9

2 Answers2

3

read() method will read until EOF, but the first program never end; read() will not return.

You need to read line by line.

Replace the following line:

data = sys.stdin.read()

with:

data = sys.stdin.readline()

In addition to that, readline() will return the read line with newline, and the print statement append new line after the string resulting empty lines inbetween. To prevent that, use sys.stdout.write:

data = sys.stdin.readline()
sys.stdout.write(data)

UPDATE

The shell will buffer output. To prevent it, flush the output:

import sys
import time

while 1:
    print "hello"
    sys.stdout.flush()
    time.sleep(0.1)

Alternatively, you can use -u option to make stdout, stderr unbuffered.

python -u send.py | python listen.py
falsetru
  • 357,413
  • 63
  • 732
  • 636
  • Nope. I just tried. still doesnt work. import sys # Listen.py while(1) : print "reading.." data = sys.stdin.readline() print data – ovfstack Oct 31 '14 at 14:51
  • This is not working because `readline` is still blocking until it will receive a `\n` because the output is finished the `readline` will continue blocking for ever or until a `\n` appears (well, that will be never...). – 0xGiddi Oct 31 '14 at 14:53
  • @ovfstack, You may need to flush the output. – falsetru Oct 31 '14 at 14:55
  • `readline` won't block forever, just `BLOCKSIZE / len("hello\n") * .1` seconds, or about 60 seconds. – Robᵩ Oct 31 '14 at 14:55
  • @user3211152, `print` statement write the string with a newline (`\n`) – falsetru Oct 31 '14 at 14:56
  • Also, the shell doesn't buffer the output. Python buffers its output. – Robᵩ Oct 31 '14 at 15:00
  • @Robᵩ, I mean shell pipe buffer. Try `ulimit -p`. (-p The pipe size in 512-byte blocks (this may not be set) ) – falsetru Oct 31 '14 at 15:03
-1

This is because the program has not terminated yet and the read() is waiting for more io. The pipe is like a blocking socket.

you may want to lookup python queues and make use of get_nowait()

Here is a answer that may help you: Non-blocking read on a subprocess.PIPE in python

Try to look up blocking and non-blocking io in python too.

Community
  • 1
  • 1
0xGiddi
  • 404
  • 2
  • 12