4

我正在尝试创建一个处理添加动态 gen_servers 的主管。出于某种原因,某些事情失败了,我不确定是什么。

-module(supervisor_mod).
-behaviour(supervisor).

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

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

init(_Args) ->
    {ok, {{simple_one_for_one, 10, 60},
          [{example_proc, {example_proc, start_link, []},
            permanent, brutal_kill, worker, [example_proc]}]}}.

add_child(Name)->                                                                        
    supervisor:start_child(supervisor_mod,
                           {example_proc, {example_proc, start_link, []},
                            permanent, brutal_kill, worker, [example_proc]}).

-module(example_proc).
-behaviour(gen_server).

-export([start_link/0]).
-export([init/1, handle_call/3, handle_cast/2]).

start_link() ->
    gen_server:start_link(?MODULE, [], []).

init(Args) ->
    io:format("~p (~p) started...~n", [Args, self()]),
    {ok, []}.

handle_call(alloc, From, State) ->
    {reply, From, State}.

handle_cast({free, _Ch}, State) ->
    {noreply, State}.

在 erl 外壳中:

Eshell V5.8.2  (abort with ^G)
1> supervisor_mod:start_link().
{ok,{ok,<0.33.0>}}
2> supervisor_mod:add_child(aa).
{error,{'EXIT',{badarg,[{erlang,apply,
                                [example_proc,start_link,
                                 {example_proc,{example_proc,start_link,[]},
                                               permanent,brutal_kill,worker,
                                               [example_proc]}]},
                        {supervisor,do_start_child_i,3},
                        {supervisor,handle_call,3},
                        {gen_server,handle_msg,5},
                        {proc_lib,init_p_do_apply,3}]}}}

任何帮助/解释/解决方案表示赞赏,/s。

4

2 回答 2

3

当您使用simple_one_for_one所有子代时,将使用相同的 ChildSpec,init/1回调中给定的一个并且此初始化只能返回一个ChildSpec。同样在这种情况下,第二个参数supervisor:start_child/2必须是列表而不是ChildSpec。该列表是附加到默认 ChildSpec 中给定的参数列表的额外参数列表,并且在调用孩子的 start 函数时使用此组合参数列表。这就是simple_one_for_one孩子们都可以使用相同的 ChildSpec 并且仍然可以获得特定参数的方式。

在您的情况下,由于 ChildSpec 中有一个空列表,并且您start_child/2使用空列表调用了 start 函数的参数总数0。这与您的start_link/0功能定义方式相匹配。

在您的情况下,另一种方法是使用one_for_one每个孩子并使用其自己的 ChildSpec 启动每个孩子。更复杂但也更通用。

不幸的是,这种双重使用supervisor:start_child/2使它的论点不一致。

于 2012-06-20T13:34:19.343 回答
3

阅读OTP 文档:在简单的一对一策略的情况下,在 start_child 函数中,您可以将参数作为子项的子 start_link 函数的列表传递。

于 2012-06-20T13:24:17.410 回答