[erlang-questions] Flush "stdout" buffer

Per Hedeland <>
Sat Sep 2 13:08:36 CEST 2017

On 2017-09-02 10:07, Michael Truog wrote:
> On 09/01/2017 04:58 AM, Frank Muller wrote:
>> Can someone help on this please?
> If you use stdout as an Erlang port like:
> STDOUT =  erlang:open_port({fd, 0, 1}, [out, {line, 256}]),
> erlang:port_command(STDOUT, "unbuffered").

Can you actually observe a difference *in buffering* between doing that
and just



> Then you don't need to flush stdout

You also don't need to flush after a call to io:format/1 as above.

> and you could probably flush stdout by just using erlang:port_command(STDOUT, "").

I can't believe that it would have any effect whatsoever.

> Doing output this way bypasses the io server

True. But the io server doesn't *buffer*, does it? Obviously there is
some amount of message passing and processing that you avoid by going
directly to a file descriptor port, but I really don't think you can
observe that difference in interactive use. If you are creating an
application for non-interactive use that will write lots of data on its
"stdout", your method is probably preferable though.

> and is useful for doing things like progress bars in Erlang source code.

I think the very trivial -eval snippet I posted in another reply
(io:format/1 interleaved with timer:sleep/1) clearly shows that you can
do progress bars just fine with io:format/1 too. But besides the
obvious, such that your progress bar will be printed on the remote node
instead of locally when running a remote shell, and that you lose the
functionality of io:format/2, I noticed a difference that I hadn't

If you are at a (e.g.) shell prompt, your method will just print on the
current line *after* the prompt, while io:format/1 will result in
"printing before" the prompt, i.e. the line is redrawn with the prompt
at the end. Both are pretty ugly but in different ways:-) though, and I
guess a progress bar is typically printed when you have given a
"command" and are waiting for the result - i.e. *not* at a prompt - in
which case there is again no observable difference.


More information about the erlang-questions mailing list