主管是否需要启动自己的主管?

发布于 2024-10-14 12:49:58 字数 132 浏览 5 评论 0原文

假设我有一个主管,我需要为该主管执行一些 start_child 。我必须首先启动所有的主管吗?或者我可以只supervisor:start_child(my_sup,[])而不启动my_sup吗?

Let us say I have one supervisor and I need execute some start_child for this supervisor. Must I first start all of start my supervisors? Or can I only supervisor:start_child(my_sup,[]) without my_sup starting?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

红尘作伴 2024-10-21 12:49:58

首先,您创建一个监督进程作为监督树的一部分,调用 supervisor :start_link/2supervisor :start_link/3。创建的supervisor进程调用Module:init/1来了解重启策略、最大重启频率和子进程规格。

这是主管启动 gen_server 的示例代码(不过,您可以启动其他gen_* 模块):

-module(ch_sup).
-behaviour(supervisor).
-export([start_link/0]).
-export([init/1]).
start_link() ->
    supervisor:start_link(ch_sup, []).
init(_Args) ->
    {ok, {{one_for_one, 1, 60},
          [{ch3, {ch3, start_link, []},
            permanent, brutal_kill, worker, [ch3]}]}}.

元组 {ch3, ...} 是一个 子规范,其定义方式如下:

{Id, StartFunc, Restart, Shutdown, Type, Modules}

上例中启动服务器 ch3 的子规范如下所示:

{ch3, {ch3, start_link, []}, permanent, brutal_kill, worker, [ch3]}

从示例中您可以看到模块 ch3 将由主管启动、监控和停止,您还可以看到 one_for_one 重启策略指定常用的。子规范中的 one_for_one 意味着如果一个子进程终止并且应该重新启动,则只有该子进程受到影响,这可能就是您的情况。您的子进程由主管自动启动、监视、重新启动和停止。

start_child/2 用于动态地将子规范添加到启动相应子进程的主管 SupRef 中。

因此,supervisor 总是首先启动,然后根据重启策略自动或手动启动其子进程。

First you create a supervisor process as part of a supervision tree calling supervisor:start_link/2 or supervisor:start_link/3. The created supervisor process calls Module:init/1 to find out about restart strategy, maximum restart frequency and child specifications.

This is the example code for a supervisor starting gen_server (though, you may start other gen_* modules):

-module(ch_sup).
-behaviour(supervisor).
-export([start_link/0]).
-export([init/1]).
start_link() ->
    supervisor:start_link(ch_sup, []).
init(_Args) ->
    {ok, {{one_for_one, 1, 60},
          [{ch3, {ch3, start_link, []},
            permanent, brutal_kill, worker, [ch3]}]}}.

The tuple {ch3, ...} is a child specification, which is defined this way:

{Id, StartFunc, Restart, Shutdown, Type, Modules}

The child specification to start the server ch3 in the example above looks like:

{ch3, {ch3, start_link, []}, permanent, brutal_kill, worker, [ch3]}

From the example you see that module ch3 will be started, monitored and stopped by supervisor, you also see one_for_one restart strategy specified which is generally used. one_for_one in the child specification means that if one child process terminates and should be restarted, only that child process is affected, and this is probably your case. Your child processes are started, monitored, restarted and stopped automatically by supervisor.

start_child/2 is used to dynamically add a child specification to the supervisor SupRef which starts the corresponding child process.

Thus supervisour is always started first, then its child processes are started automatically or manually based on the restart strategies.

柏林苍穹下 2024-10-21 12:49:58

是的,您应该首先启动主管,因为您不会启动子项,因为主管是启动子项的人。希望有帮助。

Yes, you should first start the supervisor because you will not be starting the child since the supervisor is the one which starts the child. Hope it helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文