drsalists at gmail
May 9, 2012, 1:58 PM
Post #5 of 6
You've had some good responses already, but here're two more:
1) Easiest would be to use setvbuf in the child process, if you have access
to its source. This allows you to force line-oriented buffering.
2) stdio likes to buffer to tty/pty's in a line-oriented manner, and other
things in a block-oriented manner - by default, so users get pleasing
output increments, and programs get efficiency. To trick stdio into
buffering line-oriented by default when it's sending output to another
program, you may have to talk to the child process using a pty, AKA a
pseudo terminal. There are a few ways of doing this:
A) Use CPython's pty module.
B) Use something like pexpect.
C) Check out the pty program in comp.sources.unix volume 25. This might
be pretty easy too,
assuming that pty still builds on a modern *ix.
On Wed, May 9, 2012 at 8:35 AM, Florian Lindner <mailinglists [at] xgm> wrote:
> how can I achieve a behavior like tee in Python?
> * execute an application
> * leave the output to stdout and stderr untouched
> * but capture both and save it to a file (resp. file-like object)
> I have this code
> proc = subprocess.Popen(shlex.split(cmd), stdout = subprocess.PIPE,
> while True:
> out = proc.stdout.readline()
> if out == '' and proc.poll() != None:
> This works so far but always buffers a couple of lines and outputs
> them en bloc. The final output is like it is desired but with a
> significant delay. Is there a way around that?