Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Asynchronous stdin read in Python

Tags:

python

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)
like image 822
tartifletteblvd Avatar asked Mar 05 '26 21:03

tartifletteblvd


1 Answers

Instead of

#!/usr/bin/env python

use

#!/usr/bin/env python -u
like image 143
uselpa Avatar answered Mar 08 '26 11:03

uselpa



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!