我试图创建它处理添加动态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}]}}}
任何帮助/解释/解决方案表示赞赏,/秒。