Search code examples
pythonpython-3.xpopenbuffering

subprocess.Popen: Different buffering for stdin, stdout, stderr?


I'm in need of setting the stderr stream in a Popen call to line-buffered. I discovered the bufsize argument, but it applies to all of the stdin, stdout, and stderr files.

How do I adjust the buffering to be different for each file?


Solution

  • I assume you use PIPE for stderr? In that case, I think you can do something like this:

    p = subprocess.Popen(..., stderr=subprocess.PIPE)
    fd = p.stderr.fileno()
    my_stderr = os.fdopen(os.dup(fd), 'rU', new_bufsize)
    os.close(fd)
    # use my_stderr from here on