Question

When writing codes, I ask myself which type of message should use call, which type of message should use info?

Below this question, there is another long-time doubt whether there is priority difference between info, cast, call message? whether these 3 type of messages share the same queue?

Was it helpful?

Solution

Priority of messages is the same. Quick look into gen_server.erl and you will discover simple loop receiving all data.

loop(Parent, Name, State, Mod, hibernate, Debug) ->
    proc_lib:hibernate(?MODULE,wake_hib,[Parent, Name, State, Mod, Debug]);
loop(Parent, Name, State, Mod, Time, Debug) ->
    Msg = receive
          Input ->
            Input
      after Time ->
          timeout
      end,
    decode_msg(Msg, Parent, Name, State, Mod, Time, Debug, false).

About handle_info:

This function is called by a gen_server when a timeout occurs or when it receives any other message than a synchronous or asynchronous request (or a system message).

For example timeout, tcp, udp, EXIT, sytem info and many others that dont fit into handle_call or handle_cast.

OTHER TIPS

Do you mean when to use call and when to use cast? info is used for timeouts or when other kinds of message than call and cast are received by the server.

To decide whether to use call or cast I think you should ask the question "Does the sender want to wait for an answer for this kind of message?". If yes use call which is synchronous, if no use cast which is asynchronous.

But it has been a while since I used Erlang.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top