Syl
Syl

Reputation: 13

Python: Subprocess.popen stdout: not in real time buffer

I was tring for so long to catch the stdout of a python scrypt in subprocess in real time.

SOLUTION

main.py

import subprocess
import io
import time
import sys

if __name__ == '__main__':

    command = ["python", "-u", "createValue.py"] # "-u" changed my life

    p = subprocess.Popen(command,
                         stdout=subprocess.PIPE,
                         stderr=subprocess.STDOUT)

    for line in iter(p.stdout.readline, b''):
        print(">>> " + str(line.rstrip()))

createValue.py


import time
import sys

i = 0

while i < 5:

    print("I like pizza "+ str(i))
    # sys.stdout.flush() # This is an another solution
    time.sleep(1.5)

    i += 1

Why every solution on Internet work without "-u" for them but not on my PC ? I use Python 3.6.5

Upvotes: 1

Views: 1683

Answers (1)

Bruno Assis
Bruno Assis

Reputation: 1016

Yes, it's because you need to add a environment variable called PYTHONUNBUFFERED and set it to = 1

"...Setting PYTHONUNBUFFERED to a non empty value ensures that the python output is sent straight to terminal..."

Check the solution below using your code:

import os # Added this line
import subprocess
import io
import time
import sys

if __name__ == '__main__':
    os.environ["PYTHONUNBUFFERED"] = "1" # Added
    command = ["python", "createValue.py"] # Removed "-u"

    p = subprocess.Popen(command,  
                         stdout=subprocess.PIPE,
                         stderr=subprocess.STDOUT)
    print("init")
    for line in iter(p.stdout.readline, b''):
        print(">>> " + str(line.rstrip()))

Upvotes: 3

Related Questions