Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why there is not output from spawned process show using escript

Tags:

erlang

please could you enlighten me why following code is not using stdout if run using escript?

main(_) ->                                                                                                 
    spawn(fun() -> io:fwrite("blah") end).

Thanks!

like image 804
user3169252 Avatar asked Apr 30 '26 20:04

user3169252


1 Answers

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.

like image 169
johlo Avatar answered May 03 '26 21:05

johlo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!