I want to start a python script (I will call it test.py) in another python script (run_python_script.py). For that I use the subprocess command like shown here: https://stackoverflow.com/a/18422264.
So that is run_python_script.py :
import subprocess
import sys
import io
import time
def runPython(filename, filename_log="log.txt"):
with io.open(filename_log, 'wb') as writer, io.open(filename_log, 'rb', 1) as reader:
process = subprocess.Popen("python {}".format(filename), stdout=writer)
while process.poll() is None:
sys.stdout.write(reader.read().decode("utf-8"))
time.sleep(0.5)
# Read the remaining
sys.stdout.write(reader.read().decode("utf-8"))
runPython("test.py")
And this is test.py :
import time
sleep_time = 0.0001
start_time = time.time()
for i in range(10000):
print(i)
time.sleep(sleep_time)
print(time.time() - start_time)
In this setup the live output works, but if sleep_time (in test.py) is to big, for example sleep_time = 1. run_python_script.py only outputs after test.py is finished.
I replaced time.sleep(sleep_time) with other functions and every function which takes to long, breaks the live output.
Of corse test.py is just an example. I want to use other methods, but the result is the same.