How to check if output buffering is enabled in Python

There are many ways to set output buffering in Python: Disable output buffering

What am I curious about since I know that output buffering is really really already disabled? What is the best and easiest way to test this?

+4
source share
2 answers

Try the following: $ python myscript.py | cat

If it is not loaded, the output from your script will immediately display on your terminal. Otherwise, it will be buffered with catuntil a crash occurs that is called by your script or upon completion.

+1

, , python stdout:

import sys

def is_stdout_buffered():
    # Print a single space + carriage return but no new-line (should have no visible effect)
    print " \r",
    # If the file position is a positive integer then stdout is buffered
    try:
        pos = sys.stdout.tell()
        if pos > 0:
            return True
    except IOError:  # In some terminals tell() throws IOError if stdout is unbuffered
        pass
    return False

Windows CMD, MinGW Git Bash. Git Bash, , python -u, (Cmd Git Bash Python)

, , , . , .

0

Source: https://habr.com/ru/post/1653361/


All Articles