I have two scripts.
(Code is listed below). First script is just printing numbers with a delay.
Second script is supposed to start the first one as a subprocess, then checking in the background (!) if the first script outputting a number, and printing it if so.
But what I get is that the second script printing all of the numbers at once when first one is terminated (after 20 seconds) and not one number at each moment.
Output is like:
for i in range(10):
from subprocess import Popen, PIPE, STDOUT
from time import strftime
import threading, sys
stream = Popen(['python', '1.py'], stdout=PIPE, stderr=STDOUT)
out = stream.communicate().decode("utf-8")
t = threading.Thread(target=a,)
after each printout!
You are basically writing into a buffer, and those writes don't become "flushed" out until the writer process is done! So, if you don't want to wait until the python interpreter (or the OS underneath) decides "it is time to flush" ... you have to enforce that step "manually"!
See that other answer for more background information!
EDIT: as this is not working with stream as you say: probably you have to other ways to invoke that second script. You could try using the pexpect module instead. That one exists for the "sole" purpose of "interacting" with stdout output!