Monday, October 3, 2016

Unbuffered StdOut

Occasionally I find long running and output intensive tasks often are hindered by stdout buffering.  The most apparent evidence of this is lags in the output followed by bursts of output, regardless of whether its run interactively or redirected output to a file.

The root cause is a default 4K buffer which can have performance advantages, but a hindrance if you're anxious to see the output.  This often is a point of aggravation when running lengthy tests while redirecting stdout to a file to allow:
1) actively monitoring the current state of tests and
2) providing a 'report' (often lengthy) that can be reviewed after the fact.

The following video shows an example of this buffering.  A simple python script, iterating 1024 times with a 0.05 sec delay between iterations, writing a simple message per iteration.  The desired affect is to observe each loop iteration in the output file (monitoring via 'tail -f').  You'll see however the stdout buffering gets in the way.

./run > /tmp/run.log

The stdout buffering can be disabled the 4K buffering via pre-pending the stdbuf command to the same command; 'stdbuf -oL' enables line buffering instead.

stdbuf -oL ./run > /tmp/run.log

Hope you find this useful.  Cheers.

No comments:

Post a Comment