主管是否需要启动自己的主管?
假设我有一个主管,我需要为该主管执行一些 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先,您创建一个监督进程作为监督树的一部分,调用
supervisor :start_link/2
或supervisor :start_link/3
。创建的supervisor进程调用Module:init/1
来了解重启策略、最大重启频率和子进程规格。这是主管启动 gen_server 的示例代码(不过,您可以启动其他gen_* 模块):
元组
{ch3, ...}
是一个 子规范,其定义方式如下:上例中启动服务器
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
orsupervisor:start_link/3
. The created supervisor process callsModule: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):
The tuple
{ch3, ...}
is a child specification, which is defined this way:The child specification to start the server
ch3
in the example above looks like:From the example you see that module
ch3
will be started, monitored and stopped by supervisor, you also seeone_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 supervisorSupRef
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.
是的,您应该首先启动主管,因为您不会启动子项,因为主管是启动子项的人。希望有帮助。
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.