3

A program continuously prints to standard output one line at a time.

I am trying to read and process one line at a time of this input without having to wait for the program to complete.

As an example, the below writeOutput.py writes one line at a time to stdout (waiting between each line between 1 and 3 seconds).

Calling ./writeOutput.py | ./processEachLine.py requires writeOutput.py to complete before processEachLine.py is able to start processing the first line.

Is there anyway to achieve this in python? Even by calling writeOutput.py directely within the python program instead of using a pipe?

Any help would be highly appreciated.

writeOutput.py

#!/usr/bin/env python
import random
import time

i = 0
while i < 5:  
    n = int(1 + (random.random()*10) % 3)
    i += 1
    time.sleep(n)
    print(str(n) + " test")  

processEachLine.py

#!/usr/bin/env python    
import sys

while 1:
    line = sys.stdin.readline()
    if not line:
      break
    print(">>" + line)

1 Answer 1

5

Instead of

#!/usr/bin/env python

use

#!/usr/bin/env python -u
Sign up to request clarification or add additional context in comments.

7 Comments

Thank you. #!/usr/bin/env python –u would create an error: "/usr/bin/env: python -u: No such file or directory" An easy workaround is to use python -u writeOutput.py | python -u processEachLine.py Or add #!/usr/bin/python -u Many thanks for the help.
That's strange, it works well for me (tried on OS X and Linux). What OS are you on?
This is probably necro, but the dash is probably bad - managed to recreate the error by copying the dash from tartifletteblvd's comment
@Nitz You're right, he does seem to use a character that looks like a dash but isn't.
Actually, the hashbang mechanism parses the rest of the first line as one program name (/usr/bin/env) and one argument (python -u, space included, which is not a program as you can understand). So #!/usr/bin/python -u works, while #!/usr/bin/env python -u doesn't. See also: stackoverflow.com/a/4304187/6899
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.