Search code examples
pythonstdoutstderrbufferingunbuffered

Python is not working in unbuffered mode


I'm stuggeling with an issue with python. I used Python 2.7.13 and Python 3.6.0 on Red Hat Enterprise Linux Server release 7.1 (Maipo). In order to monitor the proccesses output, I want to use tail -f to take a live look into the STDOUT and STDERR. The Keyword here is unbuffered output. Many suggestion on the internet say use python -u ... or the environment variable PYTHONUNBUFFERED like PYTHONUNBUFFERED=1 python ... or stdbuf -e0 -o0 python .... Nevertheless nothing is wokring for the following test script.

import sys
import time
while(True):
   print("Test String")
   time.sleep(1);

For all different commands I always have buffered output. Even though when I want to use STDERR. It's still buffered which really confuses me because STDERR should be unbuffered by default. Using sys.stdout.flush() or sys.stderr.flush() is also not doing the job. When using flush=True inside print() it's working as intended.

I'm looking for a solution which doesn't make it necessary to edit code because I can't edit all programs in order to get unbuffered and immediately flushed out output. How can I achieve this?

Looking forward to your answers!

Best wishes!


Solution

  • You can override print() function in Python 3. This way you will not need to change every print() function in your scripts.

    import builtins
    
    
    def print(*args):
        builtins.print(*args, sep=' ', end='\n', file=None, flush=True)
    
    
    print(
        'hello', 'world',
        'I have overrode print() function!',
        1,  # integer
        [1, 2],  # list
        {1, 2},  # set
        (1, 2),  # tuple
        {1: 2}  # dict
    )
    

    will print:

    hello world I have overrode print() function! 1 [1, 2] {1, 2} (1, 2) {1: 2}