【发布时间】:2017-06-26 16:13:45
【问题描述】:
我读了Learn Some Erlang about supervisors 并且完全迷失了。它们具有停止和终止功能
每当您想终止应用程序时,您都会关闭虚拟机的最高主管(这是通过诸如 init:stop/1 之类的函数为您完成的)。然后该主管要求其每个孩子终止。如果有些孩子是主管,他们也会这样做:
似乎发送关闭消息以接收'EXIT'确认
因此,调用 stop 来关闭进程。但是,在文本的后面,他们说必须调用 exit 函数(一个新的果实!)
当顶级主管被要求终止时,它会在每个 Pid 上调用 exit(ChildPid, shutdown)。如果孩子是工人并且陷阱退出,它将调用自己的终止函数。否则,它只会死。当主管收到关闭信号时,它会以同样的方式将其转发给自己的孩子。
最后,他们在子模块中定义了stop函数
-module(musicians).
-behaviour(gen_server).
-export([start_link/2, stop/1]).
-export([init/1, handle_call/3, handle_cast/2, handle_info/2, code_change/3, terminate/2]).
stop(Role) -> gen_server:call(Role, stop).
init:stop 是在哪里定义的?
他们还会发送停止消息
handle_call(stop, _From, S=#state{}) -> {stop, normal, ok, S};
他们的handle_info
handle_info(timeout, S = #state{name=N, skill=bad}) ->
case random:uniform(5) of
1 -> io:format("~s played a false note. Uh oh~n",[N]),
{stop, bad_note, S};
_ -> io:format("~s produced sound!~n",[N]),
{noreply, S, ?DELAY}
end;
揭示了它的回复和终止之间的联系
terminate(normal, S) ->
io:format("~s left the room (~s)~n",[S#state.name, S#state.role]);
terminate(bad_note, S) ->
io:format("~s sucks! kicked that member out of the band! (~s)~n",
[S#state.name, S#state.role]);
terminate(shutdown, S) ->
io:format("The manager is mad and fired the whole band! "
"~s just got back to playing in the subway~n", [S#state.name]);
然而,这一切看起来一团糟。你能把东西绑在一起吗?
【问题讨论】:
标签: erlang exit shutdown terminate termination