I want to get the output of the script in real time through subprocess
here is my script
import time
for i in range (10):
print(i)
time.sleep(0.5)
< hr >
here is the read script:
< H2 > script name get_output.py < / H2 >import subprocess
cmd ="/ root/test_echo.py"
p = subprocess.Popen (["python",cmd], stdout=subprocess.PIPE, stderr=subprocess.STDOUT,bufsize=1)
while 1:
line = p.stdout.readline()
if line != "" and p.poll() is None:
print line
else:
break
< hr >
I debug the above content with pycharm and can normally read the contents of test_echo line by line (real-time). However, when I put the script on centos or mac under the shell environment, it cannot be read line by line through python get_output.py. Instead, I want to know how to read line by line in the terminal as in pycharm, or how to configure subprocess
when the result of test_echo.py is finished. I want to know how to read it line by line as in pycharm.