Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Erlang 如何使用非gen_服务器的工作人员运行主管?_Erlang_Erlang Supervisor - Fatal编程技术网

Erlang 如何使用非gen_服务器的工作人员运行主管?

Erlang 如何使用非gen_服务器的工作人员运行主管?,erlang,erlang-supervisor,Erlang,Erlang Supervisor,您好,我正在尝试运行一个主管,其工作人员不是gen\u服务器。 为简洁起见,我的主管与员工在同一模块中定义: 我一直收到这个错误,我尝试将MFA属性放在[]中,但没有效果。我还将ChildSpec放在[]中。 我错过了什么 我不希望我的主管在启动时有任何工人 错误 > X=sup:start_link(). > ** exception error: no match of right hand side value {error, >

您好,我正在尝试运行一个
主管
,其工作人员不是
gen\u服务器
。 为简洁起见,我的主管与员工在同一模块中定义:

我一直收到这个错误,我尝试将
MFA
属性放在
[]
中,但没有效果。我还将
ChildSpec
放在
[]
中。 我错过了什么

我不希望我的主管在启动时有任何工人

错误

>  X=sup:start_link().
> ** exception error: no match of right hand side value {error,
>                                                        {bad_start_spec,[]}}
>      in function  sup:start_link/0 (c:/Erlang/ProcessPool/sup.erl, line 6)
> =CRASH REPORT==== 5-Apr-2020::22:20:32.918000 ===   crasher:
>     initial call: supervisor:sup/1
>     pid: <0.280.0>
>     registered_name: []
>     exception exit: {bad_start_spec,[]}
>       in function  gen_server:init_it/6 (gen_server.erl, line 358)
>     ancestors: [<0.273.0>]
>     message_queue_len: 0
>     messages: []
>     links: [<0.273.0>]
>     dictionary: []
>     trap_exit: true
>     status: running
>     heap_size: 376
>     stack_size: 27
>     reductions: 205   neighbours:

当您使用
simple\u one\u for\u one
时,您应该在
init
中定义
ChildSpec
,并且所有子级都使用相同的
ChildSpec

如果你需要不同的方法,那就用“一对一”策略代替

对于
simple\u one\u For\u one

supervisor:start\u child/2
的第二个参数必须是一个列表,该列表将与
ChildSpec
中定义的子启动函数参数的默认参数相结合

在这里,我快速修改了代码,使其适合您

-module(sup).
-behaviour(supervisor).
-compile([export_all]).

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

init(_Args) ->
     RestartStrategy = {simple_one_for_one, 10, 60},
     ChildSpec = {
                  ch1, 
                  {sup, start, []},
                  permanent,
                  brutal_kill, 
                  worker,
                  [sup]
                },
    {ok, {RestartStrategy,[ChildSpec]}}.

add(Sup,Value)->                
    supervisor:start_child(Sup,[Value]).


start(Value)->                                    
    P = spawn(?MODULE, initworker,[]),
    P!{self(),Value},
    {ok,P}.

initworker()->                            
    receive 
       {From, MSG} -> io:format(" Value is ~p~n", [MSG])
    end.

当您使用
simple\u one\u for\u one
时,您应该在
init
中定义
ChildSpec
,并且所有子级都使用相同的
ChildSpec

如果你需要不同的方法,那就用“一对一”策略代替

对于
simple\u one\u For\u one

supervisor:start\u child/2
的第二个参数必须是一个列表,该列表将与
ChildSpec
中定义的子启动函数参数的默认参数相结合

在这里,我快速修改了代码,使其适合您

-module(sup).
-behaviour(supervisor).
-compile([export_all]).

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

init(_Args) ->
     RestartStrategy = {simple_one_for_one, 10, 60},
     ChildSpec = {
                  ch1, 
                  {sup, start, []},
                  permanent,
                  brutal_kill, 
                  worker,
                  [sup]
                },
    {ok, {RestartStrategy,[ChildSpec]}}.

add(Sup,Value)->                
    supervisor:start_child(Sup,[Value]).


start(Value)->                                    
    P = spawn(?MODULE, initworker,[]),
    P!{self(),Value},
    {ok,P}.

initworker()->                            
    receive 
       {From, MSG} -> io:format(" Value is ~p~n", [MSG])
    end.

我不明白。不能在
init
之后添加
ChildSpec
吗?我的意思是,当我使用
supervisor:start\u child
时,我不能设置它的子规范吗?在添加任何子规范之前,监督员是否需要知道该规范?使用不同的代码启动子规范与“simple\u one for\u one”监督员的工作方式背道而驰。请检查这里的文件。正如我在回答中所说的,您可以尝试“一对一”主管在“init”之后使用ChildSpec。更多信息:好的,但是有没有一种方法可以将my
ChildSpec
中的数据注入到随后生成的每个子级中?在我的示例中,我想在
init()
MFA
中添加一些参数,这些参数将传递给所有添加的子级。如果在init之后需要ChildSpec。修改代码,使用一个_替换一个,如下所示。如果调用sup:add(sup:start_link(),“newargument”),答案中的或以上代码也可以动态添加参数。我不明白。不能在
init
之后添加
ChildSpec
?我的意思是,当我使用
supervisor:start\u child
时,我不能设置它的子规范吗?在添加任何子规范之前,监督员是否需要知道该规范?使用不同的代码启动子规范与“simple\u one for\u one”监督员的工作方式背道而驰。请检查这里的文件。正如我在回答中所说的,您可以尝试“一对一”主管在“init”之后使用ChildSpec。更多信息:好的,但是有没有一种方法可以将my
ChildSpec
中的数据注入到随后生成的每个子级中?在我的示例中,我想在
init()
MFA
中添加一些参数,这些参数将传递给所有添加的子级。如果在init之后需要ChildSpec。修改代码,使用一个_替换一个,如下所示。如果调用sup:add(sup:start\u link(),“newargument”),则答案中的或以上代码也可以动态添加参数。