Erlang主管不会重启孩子

时间:2016-12-12 21:29:31

标签: erlang supervisor fault-tolerance

我正在尝试了解erlang主管。我有一个简单的printer process,每3秒打印 hello 。我还有一个主管,如果发生任何异常,必须重新启动printer process

这是我的代码:

test.erl:

-module(test).
-export([start_link/0]).    

start_link() ->
    io:format("started~n"),
    Pid = spawn_link(fun() -> loop() end),
    {ok, Pid}.    

loop() ->
    timer:sleep(3000),
    io:format("hello~n"),
    loop().

test_sup.erl:

-module(test_sup).
-behaviour(supervisor).

-export([start_link/0]).
-export([init/1]).

start_link() ->
    supervisor:start_link({local, ?MODULE}, ?MODULE, []).    

init(_Args) ->
    SupFlags = #{strategy => one_for_one, intensity => 1, period => 5},    
    ChildSpecs = [#{id => test,
                    start => {test, start_link, []},
                    restart => permanent,
                    shutdown => brutal_kill,
                    type => worker,
                    modules => [test]}],    
    {ok, {SupFlags, ChildSpecs}}.

现在我运行这个程序并使用test_sup:start_link().命令启动主管,几秒钟后,我引发异常。为什么主管不重启printer process

这是shell输出:

1> test_sup:start_link().
started
{ok,<0.36.0>}
hello
hello
hello
hello          
2> erlang:error(err).

=ERROR REPORT==== 13-Dec-2016::00:57:10 ===
** Generic server test_sup terminating 
** Last message in was {'EXIT',<0.34.0>,
                           {err,
                               [{erl_eval,do_apply,6,
                                    [{file,"erl_eval.erl"},{line,674}]},
                                {shell,exprs,7,
                                    [{file,"shell.erl"},{line,686}]},
                                {shell,eval_exprs,7,
                                    [{file,"shell.erl"},{line,641}]},
                                {shell,eval_loop,3,
                                    [{file,"shell.erl"},{line,626}]}]}}
** When Server state == {state,
                            {local,test_sup},
                            one_for_one,
                            [{child,<0.37.0>,test,
                                 {test,start_link,[]},
                                 permanent,brutal_kill,worker,
                                 [test]}],
                            undefined,1,5,[],0,test_sup,[]}
** Reason for termination == 
** {err,[{erl_eval,do_apply,6,[{file,"erl_eval.erl"},{line,674}]},
         {shell,exprs,7,[{file,"shell.erl"},{line,686}]},
         {shell,eval_exprs,7,[{file,"shell.erl"},{line,641}]},
         {shell,eval_loop,3,[{file,"shell.erl"},{line,626}]}]}
** exception error: err

2 个答案:

答案 0 :(得分:3)

以下是您使用文件创建的体系结构:

test_sup (supervisor)
  ^
  |
  v
 test (worker)

然后通过调用shell中的start_link()启动您的主管。这会创建另一个双向链接:

shell
  ^
  |
  v
test_sup (supervisor)
  ^
  |
  v
 test (worker)

使用双向链接,如果任何一方死亡,另一方将被杀死。

当您运行erlang:error时,您的shell中会出错!

您的shell与您的主管相关联,因此Erlang会在响应中杀死主管。通过连锁反应,你的工人也会被杀死。

我认为您打算将错误条件发送给您的工作人员而不是shell:

  1. 确定工人的Pid:supervisor:which_children
  2. 在工人的Pid上致电erlang:exit(Pid, Reason)

答案 1 :(得分:2)

当你执行erlang:error(err).时,你正在杀死调用进程,你的shell。

由于你已经使用start_link来启动主管,它也会被杀死,循环也是。

shell会自动重启(感谢某个主管),但是没有人重新启动你的测试主管,它无法重启循环。

要进行此测试,您应该:

模块测试中的

start_link() ->
    Pid = spawn_link(fun() -> loop() end),
    io:format("started ~p~n",[Pid]),
    {ok, Pid}.

你会收到提示:

started <0,xx,0>

其中<0,xx,0>是循环pid,在shell中你可以调用

exit(pid(0,xx,0), err).

仅杀死循环。

相关问题