Jul-03-2018, 11:40 PM
You can do this using threads:
import subprocess import os from threading import Thread def std_output(pipe): with pipe: for line in iter(pipe.readline, b''): print('o ' + str(line)) def err_output(pipe): with pipe: for line in iter(pipe.readline, b''): print('e ' + str(line)) my_cmd = ['ping', 'www.google.com'] p = subprocess.Popen( my_cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, bufsize=1, preexec_fn=os.setsid ) th_out = Thread(target=std_output, args=(p.stdout,)) th_err = Thread(target=err_output, args=(p.stderr,)) th_out.start() th_err.start()