Matt Joiner
Matt Joiner

Reputation: 118710

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?

Upvotes: 3

Views: 2026

Answers (2)

Jonathan
Jonathan

Reputation: 11

If you intend to write stdout/stderr to a file in the end anyway and you just need the output to be unbuffered you could use the following:

LOG_FILE = codecs.open('somelog.txt', 'a', 'utf_8', buffering=0)

subprocess.Popen(ARGS, stdout = LOG_FILE, stderr = LOG_FILE).communicate()

Then the buffering used will be the one for the file, in this case: no buffering.

Upvotes: 1

Soulman
Soulman

Reputation: 3050

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

Upvotes: 3

Related Questions