Explain Codes LogoExplain Codes Logo

How can I flush the output of the print function?

python
best-practices
performance
tools
Nikita BarsukovbyNikita Barsukov·Sep 16, 2024
TLDR

To get immediate print output, use flush=True:

print("Immediate output!", flush=True)

To apply flushing to a custom print function:

def rapid_print(*args): print(*args, flush=True) rapid_print("Immediate output!")

For Python versions prior to 3.3, flush output manually:

import sys print("Immediate output!") sys.stdout.flush()

Applying unbuffered output to scripts

You can run your script with the -u flag for unbuffered output on-all-the-time:

python -u my_script.py

To globally unbuffer session, use PYTHONUNBUFFERED environment variable:

export PYTHONUNBUFFERED=x python my_script.py

Judicious use of sys.stdout

Understanding sys.stdout is pivotal as it's connected to the standard output. By default, sys.stdout is line-buffered. This means it's flushed when a newline char is printed. Full buffering flushes only when the buffer is full. If you need immediate output, flushing is essential.

import sys print("Part one. ") # Part one is in buffer limbo. sys.stdout.flush() # Manual call to limbo, Part one is out! print("Part two, Blazing fast.") # Immediately after Part one, Part two is out like Flash!

Nifty tricks to boost print output

Redefining print in Python 2

You can automatically include flushing within the print function:

def print_now(*args): print(*args) # Flushing off memory cobwebs, ensuring zippy output. sys.stdout.flush() print_now("Hey look, I got flushed!")

Module-wide auto-flush with functools.partial

Modify print across entire module using functools.partial, can be handy in debugging.

from functools import partial print = partial(print, flush=True) print("I get auto-flushed, every single time!")

Redirect the course of print output

Print output can flow to different streams using the file argument:

with open('log.txt', 'a') as f: print("This message is now in a bottle (log file)!", file=f)

Customize your print outs

Use sep and end to customize output formatting:

print("One","Two", sep=" then ", end=". Easy as ABC\n") # Output: One then Two. Easy as ABC

Caveats to watch out for

  • Script exécution speed: Consistent flushing can put brakes on your script due to reduced buffering efficiency.
  • I/O blocking: In case the script is writing to an output that can block, over flushing could lead to script hanging until the I/O operation is complete.
  • Resource management: Output without buffering translates to more write operations to the file or terminal which is resource-wise less efficient.