There is a python script:

from time import sleep sleep(5) print 'some_line1' sleep(5) print 'some_line2' 

Displays two lines with a delay of 5 seconds.

Redirect output to file:

 # python script.py > outfile.txt 

We outfile.txt our two lines in the file outfile.txt , but only after 10 seconds, i.e. after full execution of the script, for all 10 seconds the file was empty.

The question is how to get this data to the file instantly, because there may be not two lines, but tens of thousands, and you do not want to wait for the complete execution of the script.

    1 answer 1

    This is the effect of changing the type of buffering when redirecting standard output to a file. It is necessary to force the output buffer at the right time.

    For Python, see, for example, https://stackoverflow.com/questions/230751/how-to-flush-output-of-python-print or google (see the appropriate dock for the correct version). Example:

     $ python -u script.py > outfile.txt 

    -u command line option turns off buffers on standard I / O streams.

    In general, "legs grow" from the behavior of FILE * (libc). For a deeper understanding, I can advise you to read man setbuf .

    • Added after each print sys.stdout.flush() , works fine. Thank! - Andrey Lays