Unbuffered stdout/auto-flush

Y

Yang Zhang

Hi, is there any way to get unbuffered stdout/stderr without relying on
the -u flag to python or calling .flush() on each print (including
indirect hacks like replacing sys.stdout with a wrapper that succeeds
each write() with a flush())? Thanks in advance!
 
S

s0suk3

Hi, is there any way to get unbuffered stdout/stderr without relying on
the -u flag to python or calling .flush() on each print (including
indirect hacks like replacing sys.stdout with a wrapper that succeeds
each write() with a flush())? Thanks in advance!

I think the only way is to reopen the stdout file descriptor:

import sys
import os

# reopen stdout file descriptor with write mode
# and 0 as the buffer size (unbuffered)
sys.stdout = os.fdopen(sys.stdout.fileno(), 'w', 0)

print "unbuffered text"

Sebastian
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top