[erlang-questions] escript cutting output

OvermindDL1 <>
Thu Nov 17 22:02:57 CET 2011


io:format sends to another Erlang process and returns immediately.  If you
then exit you can cause the emulator to die before the file process has
fully flushed its data.  You just need to stay alive long enough for the
other process to do its work, it is not wrong, remember that Erlang is
designed to be a concurrent/distributed system.

I do, however, wish that there was an io:format/3 that is like io:format/2
but with one extra parameter at the end that lets you either pass in a
positive integer or the atom infinite to wait that length of time and when
it flushes it then returns (unless it times out first if not infinite), or
you can pass in a pid (or pass in a {pid(), ref()}) and it will send a
message to that process when the flush is complete.  I wish we had a lot of
io functions like that...
On Nov 17, 2011 2:56 AM, "Samuel" <> wrote:

> > 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.
>
> Best
> --
> Samuel
> _______________________________________________
> erlang-questions mailing list
> 
> http://erlang.org/mailman/listinfo/erlang-questions
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20111117/77b5ef25/attachment.html>


More information about the erlang-questions mailing list