Send gen_server message before init

I'm new to erlang, I have code:

-module (test_srv).
-behaviour (gen_server).
-export ([start_link/0, init/1, handle_info/2]).

start_link() ->
  gen_server:start_link(?MODULE, [], []).

init([]) ->
  self() ! asdasd,
  {ok, new_state}.

handle_info(Msg, State) ->
  io:format("server got ~p , now state is ~p~n", [Msg, State]),
  {noreply, State}.

I am testing in the erl shell:

1> {_, P} = test_srv:start_link().
server got asdasd , now state is new_state

The problem is that when sending a message to the server, when the server is not initialized and cannot be read, how does the gen_server dose process the message? I have the following assumptions:

  • gen_server will immediately process the message and send the message to the callback handle_info, but lose the initialized state in initthe callback

  • gen_server saves the message if it is not initialized by the server, and sends the message after server initialization.

I want to know how to deal with this erlang or gen_server problem? What is the principle of message processing?

+4
source share
1

, server is not initialised init. . , handle_info init. gen_server - , init, , init, gen_server init.

+4

Source: https://habr.com/ru/post/1657704/


All Articles