Eralng简单
我有一个测试模块和一个simple_one_for_one主管。
test.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重新启动2次,但它永远不会。  问题是什么? 
这是shell:
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是针对动态儿童的,当你请求孩子时,通过传递额外的启动参数来动态定义儿童。  其他主管可以重新启动他们的子项,因为启动参数在主管中是静态定义的。 
基本上,这种类型的主管严格地确保在需要拥有一个动态的工作人员池时进行整齐关闭。
链接地址: http://www.djcxy.com/p/38267.html上一篇: Eralng simple
