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