无法从shell产生一个Erlang主管 [英] Cannot spawn an erlang supervisor from the shell
问题描述
我已经实现了gen_server和超级用户:test_server
和test_sup
.我想从shell/CLI中测试它们.我已经编写了它们的start_link
函数,以便其名称在本地注册.
I've implemented a gen_server and supervisor: test_server
and test_sup
. I want to test them from the shell/CLI. I've written their start_link
functions such that their names are registered locally.
我发现我可以从命令行生成test_server
很好,但是生成的test_sup
根本不允许我与服务器进行交互.
I've found that I can spawn the test_server
from the command line just fine, but a spawned test_sup
does not allow me to interact with the server at all.
例如,我可以通过执行以下操作来生成test_server
:
For example, I can spawn a test_server
by executing:
1> spawn(test_server, start_link, []).
<0.39.0>
2> registered().
[...,test_server,...]
我可以与服务器进行交互,一切看起来都很好.
I can interact with the server, and everything appears fine.
但是,如果我尝试对test_sup
做同样的事情,则不会在我的"CLI进程"(使用registered/0
)中注册任何新名称/标识.我的test_server
似乎已经生成,但是我无法与其进行交互(请参阅Lukas Larsson对SASL的评论,以了解为什么这是真的).
However, if I try to do the same thing with test_sup
, no new names/Pids are registered in my "CLI process" (using registered/0
). My test_server
appears to have been spawned, but I cannot interact with it (see Lukas Larsson's comment about SASL to see why this is true).
我以为我在主管中编码了一个错误,但是这种启动主管的方法可以很好地工作:
I'd assume I coded an error in my supervisor, but this method of starting my supervisor works perfectly fine:
1> {ok, Pid}= test_sup:start_link([]).
{ok, <0.39.0>}
2> unlink(Pid).
true
3> registered().
[...,test_server,test_sup,...]
为什么我可以生成gen_server但不能生成主管?
Why is it that I can spawn a gen_server but not a supervisor?
更新
我正在使用的代码可以在这篇文章中找到.我正在使用echo_server
和echo_sup
这两个非常简单的模块.
The code I'm using can be found in this post. I'm using echo_server
and echo_sup
, two very simple modules.
给出该代码,此方法有效:
Given that code, this works:
spawn(echo_server, start_link, []).
而这不是:
spawn(echo_sup, start_link, []).
推荐答案
This explanation was given by Bernard Duggan on the Erlang questions mailing list:
链接的流程不会自动 当它们链接到一个进程时死亡 以代码正常"退出.这就是为什么 [echo_server]在以下情况下不退出 生成过程退出.那为什么呢 主管死了吗内部的 主管模块实际上是 自己实现为 gen_server,但是 process_flag(trap_exit,true)设置. 其结果是,当 父进程死亡,terminate()得到 称为(当 trap_exit已禁用),并且 主管关闭.这说得通 在主管的背景下, 主管由其父母催生 在监督树中-如果没有 每当其父关闭时就死掉 不管什么原因,你都会有 悬在树上的树枝".
Linked processes don't automatically die when a process they are linked to exits with code 'normal'. That's why [echo_server] doesn't exit when the spawning process exits. So why does the supervisor die? The internals of the supervisor module are in fact themselves implemented as a gen_server, but with process_flag(trap_exit, true) set. The result of this is that when the parent process dies, terminate() gets called (which doesn't happen when trap_exit is disabled) and the supervisor shuts down. It makes sense in the context of a supervisor, since a supervisor is spawned by its parent in a supervision tree - if it didn't die whenever its parent shutdown, whatever the reason, you'd have dangling "branches" of the tree.
这篇关于无法从shell产生一个Erlang主管的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!