Redirect log to file before process finished
Question: test.py (work):

    
    
    import time
    
    _, a, b = [1, 2, 3]
    print a
    print b
    

run the code: python test.py > test.log

you will get the log in test.log

test.py (not work):

    
    
    import time
    
    _, a, b = [1, 2, 3]
    print a
    print b
    
    while True:
        time.sleep(5)
    

But this one you get None in the log.

How do I get log before the program finished, without the python log
module(just use the redirect '>')?


Answer: Python buffers `stdout` by default so the log gets written to disk in chunks.
You can turn off the buffering a few different ways, here are two. You can use
the `-u` option when you call the script, ie:

    
    
    python -u test.py
    

You can use the enviornment varialbe `PYTHONUNBUFFERED`:

    
    
    export PYTHONUNBUFFERED=true
    python test.py
    

