0

I have problems of reading with python from stdin if I have a pipe with continuous data stream which doesn't stop.

As an example I have data_stream.py

import time

i = 0
while True:
    print(i)
    i += 1
    time.sleep(2)

Now I try to read the data with the file read_data.py

import sys
for line in sys.stdin:
    print(line)

When I try to run it with python3 data_stream.py | python3 read_data.py I get no result because data_stream.py did not finished. How can I read from data_stream.py while it is still running?

Konstantin
  • 67
  • 6
  • This code doesn't run, because (a) you haven't imported `sys`, and (b) you don't have a colon on the end of that `for`. Without code that demonstrates your problem, we can't debug your code. Also, it would really help to know what platform you're on. Please read [mcve] in the help for guidance on what goes into a question. – abarnert May 17 '18 at 21:32
  • This is your answer: https://stackoverflow.com/questions/7091413/how-do-you-read-from-stdin-in-python-from-a-pipe-which-has-no-ending?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa – MoxieBall May 17 '18 at 21:49
  • Pipes **always** feed into the reading program while the writing one is still running, but that doesn't mean that writes are always immediately visible to readers without either a certain length limit being met or an explicit flush. (In *most* languages, writes to stdout are line-buffered by default when stdout is a TTY, but block-size-chunk buffered by default when it's a FIFO). See [BashFAQ #9](https://mywiki.wooledge.org/BashFAQ/009) for a non-Python-specific discussion of the issue at hand. – Charles Duffy May 17 '18 at 22:16

1 Answers1

1

You must “flush” the stdout in data_stream.py and “readline” from read_data.py. The complete code are below:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
#  data_stream.py

import sys
import time

i = 0
while True:
    print(i)
    i += 1
    sys.stdout.flush()
    time.sleep(2)

And the code for read_data.py

#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
#  read_data.py

import sys

# Now I try to read the data with the file read_data.py

while True:
    line = sys.stdin.readline()
    print (line),

Best Regards,

  • In [How to Answer](https://stackoverflow.com/help/how-to-answer), see the section "Answer Well-Asked Questions", and within that the bullet point regarding questions which "...have already been asked and answered many times before". – Charles Duffy May 17 '18 at 22:13
  • Many thanks Charles, I'm new here. And eventually I make some mistake. – Antonio Peixoto May 17 '18 at 23:44