Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #38130

autoflush on/off

From Jabba Laci <jabba.laci@gmail.com>
Date 2013-02-04 18:12 +0100
Subject autoflush on/off
Newsgroups comp.lang.python
Message-ID <mailman.1326.1359997988.2939.python-list@python.org> (permalink)

Show all headers | View raw


Hi,

I'd like to set autoflush on/off in my script. I have a loop that is
checking something and every 5 second I want to print a '.' (dot). I
do it with sys.stdout.write and since there is no newline, it is
buffered and not visible immediately. I have this solution to use
unbuffered output:

autoflush_on = False

def unbuffered():
    """Switch autoflush on."""
    global autoflush_on
    # reopen stdout file descriptor with write mode
    # and 0 as the buffer size (unbuffered)
    if not autoflush_on:
        sys.stdout = os.fdopen(sys.stdout.fileno(), 'w', 0)
        autoflush_on = True

I call unbuffered() once and it works well. However, when this loop is
over, I'd like to set the output back to buffered. How to do that? As
far as I remember, in Perl it was simply $| = 1 and $| = 0. Can it
also be switched back and forth in Python?

Thanks,

Laszlo

Back to comp.lang.python | Previous | NextNext in thread | Find similar | Unroll thread


Thread

autoflush on/off Jabba Laci <jabba.laci@gmail.com> - 2013-02-04 18:12 +0100
  Re: autoflush on/off garabik-news-2005-05@kassiopeia.juls.savba.sk - 2013-02-04 18:51 +0000
  Re: autoflush on/off Ulrich Eckhardt <ulrich.eckhardt@dominolaser.com> - 2013-02-04 19:24 +0100

csiph-web