ViaQ / watches-cli

:watch: CLI tool to pull statistics from Elasticsearch
Apache License 2.0
1 stars 1 forks source link

Shall we force unbuffered output for python? #20

Closed lukas-vlcek closed 7 years ago

lukas-vlcek commented 7 years ago

When redirecting watches output to a file, i.e.:

$ cat /dev/null > ${log_file}
# poll cluster_health every second for 30 seconds
$ watches cluster_health -i 1 -d 30 -l >> ${log_file}

then the content in the file ${log_file} appears after the watches command finished (in this example after 30 seconds). I am not sure if there are any benefits for production use but at least for testing this is not practical. I would like to be able to tail -f ${log_file} right away and see content in the real-time.

If I understand correctly this could be solved by forcing unbuffered output for python. See http://stackoverflow.com/questions/107705/disable-output-buffering.

@richm WDYT?

richm commented 7 years ago

Yes, there should be an unbuffered output option for watches, for exactly this use case. You can also use the stdbuf command when running commands from the shell:

$ stdbuf -o 0 watches cluster_health -i 1 -d 30 -l >> ${log_file}
lukas-vlcek commented 7 years ago

After discussing it with @richm we can also add new option to do unbuffered in python code using:

import os
import sys

unbuffered = os.fdopen(sys.stdout.fileno(), 'w', 0)
sys.stdout = unbuffered

See http://stackoverflow.com/questions/881696/unbuffered-stdout-in-python-as-in-python-u-from-within-the-program