Unbuffered stdout/auto-flush

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Yang Zhang

    Unbuffered stdout/auto-flush

    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!
    --
    Yang Zhang

  • s0suk3@gmail.com

    #2
    Re: Unbuffered stdout/auto-flush

    On Jun 21, 12:29 pm, Yang Zhang <yanghates...@g mail.comwrote:
    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.s tdout.fileno(), 'w', 0)

    print "unbuffered text"

    Sebastian

    Comment

    Working...