[erlang-questions] escript cutting output

Samuel samuelrivas@REDACTED
Thu Nov 17 10:56:14 CET 2011

> Erlang IO is handled by a separate process. It is probably still
> outputting when your escript exits.
> Using "|cat" speed up the output.
> Using timer:sleep() wait for it to finish.
> Both are right. You are not doing anything wrong.

My feeling is that using cat makes it work until I output enough so
that cat isn't fast enough either. Anyway, it feels like a hack,
basically I'm redirecting output to cat so that it could handle it
because the escript fails at it.

I disagree the sleep solution is right. That's waiting a fixed period
of time that turns out to be greater than the time I should really
wait for the io system to finish flushing everything. One bad thing is
that I'm wasting a lot of time (the real escript run in a for loop in
a shell script) and other bad thing is that it can go wrong anyway if
I try to minimise the time I'm sleeping.

I'd like a solution that allowed me to wait until the exact moment
(roughly) all io has been done.

Researching a bit more, if I use erl -eval instead of an escript this
example works as I expect:

erl -eval 'io:format("~p~p~n", [lists:duplicate(100000, $a), b]), halt(0).'

The vm waits until io finishes before evaluating halt. But if I use
-noshell then the output is cut again.

Curiously, in the later case it work again if I use init:stop(0).
instead of halt(0), so I thought that init:stop effectively waited
until the io system had flushed everything, but that didn't work in my
escript either:

erl -eval 'io:format("~p~p~n", [lists:duplicate(100000, $a), b]),
init:stop().' -noshell

So my current idea is to get rid of the escript, compile main into a
module and use erl -eval with init:stop from a shell script, but I
still think it would be reasonable to be able to wait for io in
escripts, it sounds weird otherwise.


More information about the erlang-questions mailing list