tee-like behavior in Python

Dan Stromberg drsalists at gmail.com
Wed May 9 16:58:05 EDT 2012


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.de> wrote:

> Hello,
>
> 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,
> stderr=subprocess.STDOUT)
> while True:
>    out = proc.stdout.readline()
>    if out == '' and proc.poll() != None:
>       break
>    sys.stdout.write(out)
>    logfile.write(out)
>
> 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?
>
> Thanks,
>
> Florian
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20120509/64d32343/attachment-0001.html>


More information about the Python-list mailing list