为什么我的主管在初始化时使用badarg失败?

问题描述 投票:1回答:1

我正试图用一个孩子来启动类型为one_for_one的主管,并且出现此错误:

A = pl:start().
{error,{badarg,[{erlang,apply,[{state,[0|1]},init,[[]]],[]},
                {supervisor,init,1,[{file,"supervisor.erl"},{line,295}]},
                {gen_server,init_it,2,[{file,"gen_server.erl"},{line,374}]},
                {gen_server,init_it,6,[{file,"gen_server.erl"},{line,342}]},
                {proc_lib,init_p_do_apply,3,
                          [{file,"proc_lib.erl"},{line,249}]}]}}
=CRASH REPORT==== 1-Mar-2020::15:28:41.090000 ===
  crasher:
    pid: <0.215.0>
    registered_name: []
    exception error: bad argument
      in function  apply/3
         called as apply({state,[0|1]},init,[[]])
      in call from supervisor:init/1 (supervisor.erl, line 295)
      in call from gen_server:init_it/2 (gen_server.erl, line 374)
      in call from gen_server:init_it/6 (gen_server.erl, line 342)
    ancestors: [<0.209.0>]
    message_queue_len: 0
    messages: []
    links: [<0.209.0>]
    dictionary: []
    trap_exit: true
    status: running
    heap_size: 376
    stack_size: 25
    reductions: 192
  neighbours:
    neighbour:
      pid: <0.209.0>
      registered_name: []
      initial_call: {erlang,apply,2}
      current_function: {io,execute_request,2}
      ancestors: []
      message_queue_len: 0
      links: [<0.63.0>,<0.215.0>]
      trap_exit: false
      status: waiting
      heap_size: 1598
      stack_size: 25
      reductions: 5867
      current_stacktrace: [{io,execute_request,2,[{file,"io.erl"},{line,579}]},
                  {shell,exprs,7,[{file,"shell.erl"},{line,693}]},
                  {shell,eval_exprs,7,[{file,"shell.erl"},{line,642}]},
                  {shell,eval_loop,3,[{file,"shell.erl"},{line,627}]}]
** exception exit: badarg
     in function  apply/3
        called as apply({state,[0|1]},init,[[]])
     in call from supervisor:init/1 (supervisor.erl, line 295)
     in call from gen_server:init_it/2 (gen_server.erl, line 374)
     in call from gen_server:init_it/6 (gen_server.erl, line 342)
     in call from proc_lib:init_p_do_apply/3 (proc_lib.erl, line 249)

主管:

    -module(pl).
    -behaviour(supervisor).
    -export([start/0,init/1]).
    -record(state,{
        data=[]
    }).

    %% {ChildId, StartFunc, Restart, Shutdown, Type, Modules}
    start()->
        supervisor:start_link({local,?MODULE},#state{data=[0|1]},[]).

    init(State=#state{data=D})->
        InitialChild={fchild,{serv,start_link,[-1]},temporary,3000,brutal_kill,worker},
        MaxRestart=2,
        MaxTime=600,
        Strategy={one_for_one,MaxRestart,MaxTime},
        {ok,{Strategy,[
                InitialChild
        ]}}.

工人:

-module(serv).
-behaviour(gen_server).
-compile(export_all).
-define(A,300).
-record(state,{
    values=[],
    id
}).

call(Pid,Message)->
    gen_server:call(Pid,Message).

cast(Pid,Message)->
    gen_server:cast(Pid,Message).
start_link(Id)->
    {ok,Pid}=gen_server:start_link({local,xx},?MODULE,[Id],[]),
    Pid.
stop(Ref)->
    gen_server:stop(Ref).
init(Id)->
    {ok,#state{values=[],id=Id}}.
handle_call(state,From,State=#state{values=V})->
    Reply={reply,State,State},
    Reply;

[我只是试图仅由一个在其init方法中获取参数的孩子来启动主管。该代码有什么问题?

erlang otp erlang-supervisor
1个回答
4
投票

start_link的第二个参数带有3个参数,应该是模块名称;应该是

start()->
    supervisor:start_link({local,?MODULE}, ?MODULE, [#state{data=[0|1]}]).
© www.soinside.com 2019 - 2024. All rights reserved.