Unbuffered stderr in Python 3

Wolfgang Maier wolfgang.maier at biologie.uni-freiburg.de
Mon Nov 2 05:54:44 EST 2015


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)





More information about the Python-list mailing list