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


Groups > comp.lang.python > #38130 > unrolled thread

autoflush on/off

Started byJabba Laci <jabba.laci@gmail.com>
First post2013-02-04 18:12 +0100
Last post2013-02-04 19:24 +0100
Articles 3 — 3 participants

Back to article view | Back to comp.lang.python


Contents

  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

#38130 — autoflush on/off

FromJabba Laci <jabba.laci@gmail.com>
Date2013-02-04 18:12 +0100
Subjectautoflush on/off
Message-ID<mailman.1326.1359997988.2939.python-list@python.org>
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

[toc] | [next] | [standalone]


#38136

Fromgarabik-news-2005-05@kassiopeia.juls.savba.sk
Date2013-02-04 18:51 +0000
Message-ID<keovvg$fjb$1@speranza.aioe.org>
In reply to#38130
Jabba Laci <jabba.laci@gmail.com> wrote:
> 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.

My solution is sys.stdout.write('.'); sys.stdout.flush()

-- 
 -----------------------------------------------------------
| Radovan GarabĂ­k http://kassiopeia.juls.savba.sk/~garabik/ |
| __..--^^^--..__    garabik @ kassiopeia.juls.savba.sk     |
 -----------------------------------------------------------
Antivirus alert: file .signature infected by signature virus.
Hi! I'm a signature virus! Copy me into your signature file to help me spread!

[toc] | [prev] | [next] | [standalone]


#38137

FromUlrich Eckhardt <ulrich.eckhardt@dominolaser.com>
Date2013-02-04 19:24 +0100
Message-ID<sva5u9-uek.ln1@satorlaser.homedns.org>
In reply to#38130
Am 04.02.2013 18:12, schrieb Jabba Laci:
> 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

Note that you have two file objects (one not reachable any more) both 
writing to the same file descriptor. This also means you should first 
flush sys.stdout before changing it, otherwise it might still contain 
unflushed data.

> 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?

Just set sys.stdout back to the original value. OTOH, also check if you 
can't tell sys.stdout not to buffer.

> As far as I remember, in Perl it was simply $| = 1 and $| = 0.

"simply" ... ;)


Uli

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web