please could you enlighten me why following code is not using stdout if run using escript?
main(_) ->
spawn(fun() -> io:fwrite("blah") end).
Thanks!
fwrite still writes to stdout when running in an escript, the problem here is that your program terminates before the spawned process has had a chance to run!
The escript terminates as soon as the main function terminates, depending on how the virtual machine scheduled your spawned process you may or may not get the fwrite executed.
A simple workaround for your example is to add some synchronization:
main(_) ->
MainPid=self(),
spawn(fun() -> io:fwrite("blah"), MainPid ! done end),
receive
done ->
ok
end.
This makes the main process wait with termination until the spawned process has sent a message.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With