Here is what I would like to do. I would like to execute a command using subprocess.Popen(), capture the output and write it to sys.stdout. I know I can get stdout and stderr easily enough like this:
p = Popen(cmd, shell=True, bufsize=bufsize, stdout=PIPE, stderr=PIPE, close_fds=True)
(child_stdout, child_stderr) = p.communicate()
for line in child_stdin:
I want to do something a bit different and likely a bit more complicated. In the code above (if I understand things correctly), the for loop will not execute until the command executes. For a long running command this is unnaceptable to me as I would like some feedback to the user. In addition, I would like to preface every line sent from the subprocess with something along the lines of:
username@host stdout ==>
To distinguish this output from normal program output. Essentially what I need is a rewrite pipe that takes stdout from the subprocess prepends stuff to each line and outputs it to stdout, or even more generically to any FILE I give it as an agrument.
It seems like this should be fairly straightforward, but I'm not sure if I need to mess with threads or anything like that.