erlang supervisor simple_one_for_one实例
http://www.cnblogs.com/little-ant/p/3196201.html
simple_one_for_one vs one_for_one:
相同點:
這種Restart Strategy和one_for_one基本相同(即當(dāng)一個child process掛掉后,僅僅重啟該child process 而不影響其他child process)。
異同點:
1, simple_one_for_one?supvisor啟動時不會啟動任何子進程,所有子進程都只能通過調(diào)用?supervisor:start_child(Sup, Args)?來動態(tài)啟動。
2, simple_one_for_one?supvisor持有child process的定義,并有一個dict存放數(shù)據(jù), 其實就是如干個child process,共享一份數(shù)據(jù)。
3,?一個simple_one_for_one?supervisor只有一個(single)simple_one_for_one的定義, 也就是說它只能生產(chǎn)出一種類型worker process。
?
supervisor:start_child(Sup, Args) :
? ? Sup: ?Supervisor的Pid或者registered Name.
? ? Args:?當(dāng)supervisor的類型是simple_one_for_one時,Args會追加到spec的參數(shù)中。
例如:
-module(simple_sup).
-behaviour(supervisor).-export([start_link/0]).
-export([init/1]).start_link() ->supervisor:start_link({local,?MODULE}, ?MODULE, []).init(_Args) ->{M, F, A} = _Args,{ok, {{simple_one_for_one, 0, 1},[{M, {M, F, A},temporary, brutal_kill, worker, [M]}]}}. 調(diào)用supervisor:start_child(simple_sup, Args)后,最終啟動子進程的代碼會是:apply(M, F, A++Args).
調(diào)用supervisor:start_child(Sup, Args)可能會遇到的錯誤:
1, noproc: 可能的原因是在調(diào)用supervisor:start_link時沒寫參數(shù){local, ?MODULE},即上面代碼紅色部分,此時進程Sup并不存在,所以會產(chǎn)生
? ? ? ? ? ? ? ? ?noproc錯誤。
2,undef: 可能的原因是A++Args后,與child process的start_link函數(shù)參數(shù)不一致。
?
實例代碼:
? ? ?實例包含兩部分,一是監(jiān)控普通進程(normal_process.erl),二是監(jiān)控gen_server進程(gen_server_process.erl)。
common.hrl
%%-define(CALL, normal_process). -define(CALL, gen_server_process).
simple_sup.erl
-module(simple_sup).
-behaviour(supervisor).-export([start_link/0]).
-export([init/1]).-include("common.hrl").start_link() ->supervisor:start_link({local, ?MODULE}, simple_sup, []).init(_Args) ->{ok, {{simple_one_for_one, 0, 1}, [{?CALL, {?CALL, start_link, []},temporary, brutal_kill, worker, [?CALL]}]}}. normal_process.erl
-module(normal_process).-export([start_link/0, start_loop/1]).start_link() ->proc_lib:start_link(?MODULE, start_loop, [self()]).start_loop(Parent) ->proc_lib:init_ack(Parent, {ok, self()}),loop().loop() ->receiveArgs ->io:format("~p~n", [Args])end. gen_server_process.erl
-module(gen_server_process).
-behaviour(gen_server).
-export([start_link/0]).%% gen_server callbacks
-export([init/1, handle_call/3, handle_cast/2, handle_info/2,terminate/2, code_change/3]).%% interface
-export([start/0, stop/0, echo/1]).%% interface implement
start() -> start_link().
stop() -> gen_server:call(?MODULE, stop).
echo(String) -> gen_server:call(?MODULE, {echo, String}).%% gen_server callbacks implement
start_link() -> gen_server:start_link({local,?MODULE}, ?MODULE, [], []).init([]) ->{ok, 0}. handle_call({echo, String}, _From, Tab) -> Reply = String,{reply, Reply, Tab};handle_call(stop, _From, Tab) ->{stop, normal, stopped, Tab}.handle_cast(_Msg, State) -> {noreply, State}.
handle_info(_Info, State) -> {noreply, State}.
terminate(_Reason, _State) -> ok.
code_change(_OldVsn, State, _Extra) -> {ok, State}. 編譯命令:
c(simple_sup). c(normal_process). c(gen_server_process).
測試命令:
simple_sup:start_link(). supervisor:start_child(simple_sup, []).
測試通過start_child啟動普通進程:
修改common.hrl:-define(CALL, normal_process). 執(zhí)行編譯命令?+?測試命令。
測試通過start_child啟動gen_server進程:
修改common.hrl:-define(CALL, gen_server_process). 執(zhí)行編譯命令?+?測試命令。
轉(zhuǎn)載于:https://www.cnblogs.com/fvsfvs123/p/4243734.html
總結(jié)
以上是生活随笔為你收集整理的erlang supervisor simple_one_for_one实例的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Resin介绍及其使用配置
- 下一篇: 《梦仙》是哪个时期的作品?