Erlang动态supervisor启动gen_server
我有根主管创建其他主管:
start_link() ->
supervisor:start_link({local, ?MODULE}, ?MODULE, []).
init([]) ->
RestartStrategy = {one_for_one, 5, 600},
ListenerSup =
{popd_listener_sup,
{popd_listener_sup, start_link, []},
permanent, 2000, supervisor, [popd_listener]},
Children = [ListenerSup],
{ok, {RestartStrategy, Children}}.
我有 gen_server - 监听器。创建主管后,如何使用 popd_listener_sup
主管运行此 gen_server ?
谢谢。
I have root supervisor that create other supervisor:
start_link() ->
supervisor:start_link({local, ?MODULE}, ?MODULE, []).
init([]) ->
RestartStrategy = {one_for_one, 5, 600},
ListenerSup =
{popd_listener_sup,
{popd_listener_sup, start_link, []},
permanent, 2000, supervisor, [popd_listener]},
Children = [ListenerSup],
{ok, {RestartStrategy, Children}}.
And i have gen_server - listener. How can i run this gen_server with popd_listener_sup
supervisor, when supervisor created?
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
根主管
如果子进程是另一个主管,则子规范中的
Shutdown
应设置为infinity
以便为子树提供充足的关闭时间,并且Type
> 应该设置为supervisor
,这就是我们所做的。子主管
在这里,在子规范中,我们将
Shutdown
的值设置为2000
。整数超时值意味着主管将通过调用 exit(Child,shutdown) 告诉子进程终止,然后等待子进程返回带有原因关闭的退出信号。来自 Erlang/OTP 文档的监听器
:
这就是我们在
Module:init(Args)
中调用erlang:process_flag(trap_exit, true)
的原因。示例运行
启动根管理程序:
根管理程序运行并自动启动其子进程,在我们的示例中为子管理程序。子supervisor依次启动其子进程;在我们的例子中,我们只有一个孩子,
ch1
。让我们让
ch1
评估正常代码:现在一些错误的代码:
正如您所看到的,子进程
ch1
被子主管popd_listener_sup
重新启动(注意 < code>ch1 已启动 (<0.39.0>))。由于我们的 shell 和根管理程序是双向链接的(在根管理程序函数
start_link/0
中调用supervisor:start_link
,而不是supervisor:start
),exit(normal)
导致根管理程序关闭,但其子进程有一些时间进行清理。Root supervisor
If the child process is another supervisor,
Shutdown
in child specification should be set toinfinity
to give the subtree ample time to shutdown, andType
should be set tosupervisor
, and that's what we did.Child supervisor
Here, in a child specification, we set value of
Shutdown
to2000
. An integer timeout value means that the supervisor will tell the child process to terminate by callingexit(Child,shutdown)
and then wait for an exit signal with reason shutdown back from the child process.Listener
From Erlang/OTP documentation:
That's why we called
erlang:process_flag(trap_exit, true)
inModule:init(Args)
.Sample run
Starting the root supervisor:
Root supervisor is run and automatically starts its child processes, child supervisor in our case. Child supervisor in turn starts its child processes; we have only one child in our case,
ch1
.Let's make
ch1
evaluate normal code:Now some bad code:
As you may see child process
ch1
was restarted by the child supervisorpopd_listener_sup
(noticech1 has started (<0.39.0>)
).Since our shell and root supervisor are bidirectionally linked (call
supervisor:start_link
, notsupervisor:start
in the root supervisor functionstart_link/0
),exit(normal)
resulted in the root supervisor shutdown, but its child processes had some time to clean up.