![](/img/trans.png)
[英]Erlang supervisor: difference between simple_one_for_one and one_for_one restart strategies
[英]Erlang simple_one_for_one supervisor does not restart child
我有一個測試模塊和一個 simple_one_for_one 主管。
測試.erl
-module(test).
-export([
run/1,
do_job/1
]).
run(Fun) ->
test_sup:start_child([Fun]).
do_job(Fun) ->
Pid = spawn(Fun),
io:format("started ~p~n", [Pid]),
{ok, Pid}.
test_sup.erl
-module(test_sup).
-behaviour(supervisor).
-export([start_link/0]).
-export([init/1]).
-export([start_child/1]).
start_link() ->
supervisor:start_link({local, ?MODULE}, ?MODULE, []).
init(_Args) ->
SupFlags = #{strategy => simple_one_for_one, intensity => 2, period => 20},
ChildSpecs = [#{id => test,
start => {test, do_job, []},
restart => permanent,
shutdown => brutal_kill,
type => worker,
modules => [test]}],
{ok, {SupFlags, ChildSpecs}}.
start_child(Args) ->
supervisor:start_child(?MODULE, Args).
我通過命令test_sup:start_link()
在 shell 中啟動主管。 之后我運行這個命令: test:run(fun() -> erlang:throw(err) end).
我除了函數do_job
restart 2 次,但它從來沒有。 問題是什么?
這是外殼:
1> test_sup:start_link().
{ok,<0.36.0>}
2> test:run(fun() -> erlang:throw(err) end).
started <0.38.0>
{ok,<0.38.0>}
3>
=ERROR REPORT==== 16-Dec-2016::22:08:41 ===
Error in process <0.38.0> with exit value:
{{nocatch,err},[{erlang,apply,2,[]}]}
重新啟動孩子與simple_one_for_one
主管的定義方式相反。 根據主管文檔:
函數 delete_child/2 和 restart_child/2 對 simple_one_for_one 主管無效,如果指定的主管使用此重啟策略,則返回 {error,simple_one_for_one}。
換句話說,你所要求的永遠不會發生。 那是因為simple_one_for_one
用於動態子項,這些子項是通過在請求子項時傳入額外的啟動參數來動態定義的。 其他主管能夠重新啟動他們的孩子,因為啟動參數是在主管中靜態定義的。
基本上,這種類型的主管嚴格用於確保在需要動態工作人員池時有序關閉。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.