12

I recently switched OS and am using a newer Python (2.7). On my old system, I used to be able to print instantaneously. For instance, suppose I had a computationally intense for loop:

for i in range(10):
  huge calculation
  print i

then as the code completed each iteration, it would print i

However, on my current system, python seems to cache the stdout so that the terminal is blank for several minutes, after which it prints:

1
2
3

in short succession. Then, after a few more minutes, it prints:

4
5
6

and so on. How can I make python print as soon as it reaches the print statement?

agf
  • 171,228
  • 44
  • 289
  • 238
Rishi
  • 3,538
  • 5
  • 29
  • 40

3 Answers3

15

Try to call flush of stdout after the print

import sys

...
sys.stdout.flush()

Or use a command line option -u which:

Force stdin, stdout and stderr to be totally unbuffered.

Oleg Pavliv
  • 20,462
  • 7
  • 59
  • 75
  • Thanks! This works, but do you know how I can set this automatically? – Rishi Sep 18 '11 at 19:34
  • 2
    @Rishi: The closest you could get to having it automatic (without having to resort to compiling your own version of python) would be to set the `PYTHONUNBUFFERED` environment variable. – Jeff Mercado Sep 18 '11 at 19:41
  • Jeff's comment was very useful to me. Thank you – Rishi Sep 19 '11 at 00:44
14

Since Python 3.3, you can simply pass flush=True to the print function.

Tobu
  • 24,771
  • 4
  • 91
  • 98
4

Import the new print-as-function as in Python 3.x:

from __future__ import print_function

(put the statement at the top of your script/module)

This allows you to replace the new print function with your own:

def print(s, end='\n', file=sys.stdout):
    file.write(s + end)
    file.flush()

The advantage is that this way your script will work just the same when you upgrade one day to Python 3.x.

Ps1: I did not try it out, but the print-as-function might just flush by default.

PS2: you might also be interested in my progressbar example.

Community
  • 1
  • 1
Remi
  • 20,619
  • 8
  • 57
  • 41