erlang 消息发送 gen_server:call cast info

消息发送

  • 启动server

    {ok,Pid} = gen_server_example:start().  
  • 发送call消息

    Mref = erlang:monitor(process,Pid).
    erlang:send(Pid,{'$gen_call',{self(),Mref},a}).
    • 接收返回消息并关闭引用
     receive
        {Mref, Reply} ->
            erlang:demonitor(Mref, [flush]),
            {ok, Reply};
        {'DOWN', Mref, _, _, noconnection} ->
            Node = get_node(Process),
            exit({nodedown, Node});
        {'DOWN', Mref, _, _, Reason} ->
            exit(Reason)
        after Timeout ->
            erlang:demonitor(Mref, [flush]),
            exit(timeout)
        end
  • 发送cast消息

    erlang:send(Pid,{'$gen_cast',a}).
  • 发送info消息

    erlang:send(Pid,a).
  • 调试相关命令

    exit(gen_server_example,0). 
    erlang:registered().   
    Pid = whereis(gen_server_example).    

你可能感兴趣的:(erlang)