Path: csiph.com!au2pb.net!feeder.erje.net!1.eu.feeder.erje.net!news-1.dfn.de!news.dfn.de!news.informatik.hu-berlin.de!fu-berlin.de!uni-berlin.de!not-for-mail From: Wolfgang Maier Newsgroups: comp.lang.python Subject: Re: Unbuffered stderr in Python 3 Date: Mon, 2 Nov 2015 11:54:44 +0100 Lines: 29 Message-ID: References: <5637165b$0$1505$c3e8da3$5496439d@news.astraweb.com> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-Trace: news.uni-berlin.de aZ2Fpbmte76vxNp89N3ArQeJ9vKYuBH2J3KDn2u5p12Q== Return-Path: 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; 'subject:Python': 0.05; 'rewrite': 0.07; 'sys,': 0.07; 'flush': 0.09; 'mess': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'def': 0.13; 'argument': 0.15; 'boolean': 0.16; 'received:80.91.229.3': 0.16; 'received:io': 0.16; 'received:plane.gmane.org': 0.16; 'received:psf.io': 0.16; 'simulate': 0.16; 'stderr.': 0.16; 'test():': 0.16; 'wrote:': 0.16; 'import': 0.24; 'header:In-Reply- To:1': 0.24; 'header:User-Agent:1': 0.26; 'example': 0.26; 'header:X-Complaints-To:1': 0.26; 'error': 0.27; 'function': 0.28; 'this.': 0.28; 'accepts': 0.29; 'be:': 0.29; 'prints': 0.29; 'print': 0.30; 'code': 0.30; 'lets': 0.33; 'should': 0.36; 'keyword': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'received:org': 0.37; 'wrong': 0.38; 'to:addr:python.org': 0.40; 'received:de': 0.40; 'your': 0.60; 'received:93': 0.72; 'as:': 0.79 X-Injected-Via-Gmane: http://gmane.org/ X-Gmane-NNTP-Posting-Host: x5d86a1a0.dyn.telefonica.de User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Thunderbird/38.3.0 In-Reply-To: X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com comp.lang.python:98094 On 02.11.2015 11:48, Wolfgang Maier wrote: > > Since Python3.3, the print function has a flush keyword argument that > accepts a boolean and lets you do just this. Rewrite your example as: > > import sys, time > > def test(): > # Simulate a slow calculation that prints status and/or error > # messages to stderr. > for i in range(10): > _print(i, file=sys.stderr, end="", flush=True) > time.sleep(2) > print("", file=sys.stderr) > > and it should do what you want. sorry for this mess (copy pasted from the wrong source). The code should be: def test(): # Simulate a slow calculation that prints status and/or error # messages to stderr. for i in range(10): print(i, file=sys.stderr, end="", flush=True) time.sleep(2) print("", file=sys.stderr)