Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #38141
| Path | csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <python-python-list@m.gmane.org> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.000 |
| X-Spam-Evidence | '*H*': 1.00; '*S*': 0.00; 'output': 0.04; 'context': 0.05; 'that?': 0.05; 'over,': 0.07; "'.'": 0.09; "'w',": 0.09; 'descriptor': 0.09; 'exit.': 0.09; 'newline,': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'stdout': 0.09; 'sys.stdout': 0.09; 'terry': 0.09; 'def': 0.10; 'library': 0.15; 'buffering': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'reedy': 0.16; 'wrote:': 0.17; 'script.': 0.17; 'switched': 0.17; 'jan': 0.18; 'python?': 0.20; 'visible': 0.22; "i'd": 0.22; 'class.': 0.23; 'second': 0.24; 'header:In-Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; 'checking': 0.27; 'header:X-Complaints-To:1': 0.28; 'subject:/': 0.28; 'perl': 0.29; 'types.': 0.29; "skip:' 10": 0.30; 'that.': 0.30; 'mode': 0.30; 'file': 0.32; 'print': 0.32; 'to:addr:python- list': 0.33; 'hi,': 0.33; 'false': 0.35; 'pm,': 0.35; 'something': 0.35; 'there': 0.35; 'received:org': 0.36; 'except': 0.36; 'method': 0.36; 'should': 0.36; 'does': 0.37; 'drop': 0.37; 'far': 0.37; 'well.': 0.37; 'subject:: ': 0.38; 'object': 0.38; 'instead': 0.39; 'to:addr:python.org': 0.39; 'header:Received:5': 0.40; 'your': 0.60; 'skip:u 10': 0.60; 'save': 0.61; 'back': 0.62; 'subject:off': 0.65; 'restore': 0.69; 'saving': 0.72; 'forth': 0.75; 'manual,': 0.84; 'received:fios.verizon.net': 0.84; 'remember,': 0.93 |
| X-Injected-Via-Gmane | http://gmane.org/ |
| To | python-list@python.org |
| From | Terry Reedy <tjreedy@udel.edu> |
| Subject | Re: autoflush on/off |
| Date | Mon, 04 Feb 2013 16:13:50 -0500 |
| References | <CAOuJsMmQ55vjG_TYU2iPNHeA=b-kGX+cWa2xFDXp4j6WfQAp3Q@mail.gmail.com> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset=UTF-8; format=flowed |
| Content-Transfer-Encoding | 7bit |
| X-Gmane-NNTP-Posting-Host | pool-173-75-251-66.phlapa.fios.verizon.net |
| User-Agent | Mozilla/5.0 (Windows NT 6.1; WOW64; rv:17.0) Gecko/17.0 Thunderbird/17.0 |
| In-Reply-To | <CAOuJsMmQ55vjG_TYU2iPNHeA=b-kGX+cWa2xFDXp4j6WfQAp3Q@mail.gmail.com> |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.15 |
| Precedence | list |
| List-Id | General discussion list for the Python programming language <python-list.python.org> |
| List-Unsubscribe | <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe> |
| List-Archive | <http://mail.python.org/pipermail/python-list/> |
| List-Post | <mailto:python-list@python.org> |
| List-Help | <mailto:python-list-request@python.org?subject=help> |
| List-Subscribe | <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.1334.1360012468.2939.python-list@python.org> (permalink) |
| Lines | 36 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1360012468 news.xs4all.nl 6840 [2001:888:2000:d::a6]:48420 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:38141 |
Show key headers only | View raw
On 2/4/2013 12:12 PM, Jabba Laci 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. 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? Write a context manager class. See Library manual, 4.11. Context Manager Types. The __enter__ method would be much like the above except that is should save the old stdout object 'oldstdout = sys.stdout' instead of fiddling with 'autoflush_on'. Then __exit__ would simply be 'sys.stdout = oldstdout'. Drop autoflush_on. Your context manager should not care about the existing buffering other than to restore it on exit. Saving and restoring the existing stdout object does that. -- Terry Jan Reedy
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
Re: autoflush on/off Terry Reedy <tjreedy@udel.edu> - 2013-02-04 16:13 -0500
csiph-web