gpt4 book ai didi

elixir - Elixir 应用中有多个主管

转载 作者:行者123 更新时间:2023-12-05 04:01:57 31 4
gpt4 key购买 nike

我想在我的 Phoenix 应用程序中注册 2 个根主管,有什么理由不这样做吗?

例子如下

defmodule MyApp.Application do
use Application
import Supervisor.Spec

def start(_type, _args) do
children_sv1 = [
supervisor(MyApp.Repo, []),
... # other workers
]

children_sv2 = [
supervisor(MyApp.Endpoint, []),
... # other workers
]

opts1 = [strategy: :one_for_one, name: MyApp.Supervisor1]
opts2 = [strategy: :one_for_one, name: MyApp.Supervisor2]

Supervisor.start_link(children_sv1, opts)
Supervisor.start_link(children_sv2, opts)
end
end

最佳答案

不这样做的一个原因是您的特定应用程序本身作为 OTP 范围的监督树的一部分受到监督。 start 的返回值由顶级应用程序监督程序用来监督您的特定应用程序。

如果您明确地将结果分配给上面的主管调用,您将看到您正在删除信息:

{:ok, sup1_pid} = Supervisor.start_link(children_sv1, opts)
{:ok, sup2_pid} = Supervisor.start_link(children_sv2, opts)
{:ok, sup2_pid}

这意味着虽然第一个监督者将链接到启动您的应用程序的任何进程(例如顶级应用程序监督者),但它不会出现在查看监督树的函数的输出中,例如 Supervisor .count_children.在正常操作期间,这应该不是什么大问题,但如果出现任何问题,您自己可能很难分析问题,并且依赖于适当监督层次结构的 OTP 工具在面对这种设置时可能会表现得很奇怪。优雅地停止应用程序可能会也可能不会成为问题。

将整个监督树指定为正确的树始终是更安全的选择——您最终会得到一个更可预测的应用程序。如果您需要精细控制树的哪些分支是独立的以及它们应该如何重新启动,子规范是您最好的 friend 。

关于elixir - Elixir 应用中有多个主管,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54828145/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com