Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if output buffering is enabled in Python

Tags:

There are a lot of ways to set output buffering off in Python: Disable output buffering

What makes me curious is how do I know that output buffering is really really already off? What is the best and simple way to check it?

like image 284
Aminah Nuraini Avatar asked Sep 01 '16 19:09

Aminah Nuraini


People also ask

What is output buffering in Python?

Summary: Python output buffering is the process of storing the output of your code in buffer memory. Once the buffer is full, the output gets displayed on the standard output screen.

How do you clear the output buffer in Python?

stdout. flush() forces it to “flush” the buffer, meaning that it will write everything in the buffer to the terminal, even if normally it would wait before doing so. The sys module provides functions and variables used to manipulate different parts of the Python runtime environment.

How do I stop the output buffering?

You can turn on output buffering with ob_start() , and turn it off with ob_end_flush() or ob_end_clean() . You can also have all your scripts automatically start with output buffering on using the output_buffering option in php. ini.

What is default buffer size in Python?

DEFAULT_BUFFER_SIZE: System default buffer size, 8192 bytes (8KB). unbuffered: not use the buffer. Available in binary file mode. It means writing to hard disk in every action.


1 Answers

I've tested the stdout buffering on Linux with Python 3.8, and found that Python3's output buffer can detect if it is being run at a terminal (a "TTY"), in which case the output from print functions is displayed immediately, otherwise the output is delayed until the buffer is full or the script exits. It will decide that it's not at a TTY if the stdout is piped, for example. (Sorry, I've no idea what happens on Windows.)

This output delay can be prevented with the "flush=True" print argument, of course. It can also be prevented by disabling buffering with Python3's "-u" command-line argument, or by setting the shell environment variable PYTHONUNBUFFERED=true, in which case the buffer is replaced with direct FileIO.

All this can be detected within Python3 by querying the sys.stdout.buffer like so:

#!/usr/bin/env python3
import sys
print("Name: ", type(sys.stdout.buffer).__name__)
print("TTY? ", sys.stdout.buffer.isatty())

This script's output under the various conditions is:

$ myscript.py # Displays immediately
Name:  BufferedWriter
TTY?  True

$ myscript.py | tee # Waits for script exit because of pipe
Name:  BufferedWriter
TTY?  False

$ python3 -u myscript.py # Displays immediately
Name:  FileIO
TTY?  True

$ python3 -u myscript.py | tee # Also displays immediately despite pipe
Name:  FileIO
TTY?  False

Edit: And the buffering can also be disabled with the following shebang (assuming the script name is not prefixed with "python3" in the shell command.)

#!/usr/bin/env -S python3 -u
like image 152
Dave Rove Avatar answered Sep 24 '22 16:09

Dave Rove