gpt4 book ai didi

erlang - Erlang和process_flag(trap_exit,true)

转载 作者:行者123 更新时间:2023-12-03 13:04:04 24 4
gpt4 key购买 nike

在观看Erlang上的Pragmatic Studio屏幕广播之后,有关Supervisors的最后一个视频提到,为了使主管能够获得有关其子级之一的通知,以便可以正确地重新启动它,该子级应向process_flag(trap_exit, true)注册。也许我只是误解了作者(我误会的机会非常高),但我认为主管可以自动知道 child 何时死亡(可能是通过spawn_link或类似的背景)。这真的有必要吗?在实际情况下,何时应该使用process_flag(trap_exit,true),因为文档明确声明了以下内容:

http://www.erlang.org/doc/man/erlang.html#process_flag-2

process_flag(trap_exit, Boolean)

When trap_exit is set to true, exit signals arriving to a process are converted to {'EXIT', From, Reason} messages, which can be received as ordinary messages. If trap_exit is set to false, the process exits if it receives an exit signal other than normal and the exit signal is propagated to its linked processes. Application processes should normally not trap exits.``

最佳答案

您有3个成语:

1 /我不在乎我的 child 进程是否死亡:

spawn(...)

2 /如果我的子进程崩溃,我想崩溃:
spawn_link(...)

3 /如果我的子进程终止(我希望正常终止),我想收到一条消息:
process_flag(trap_exit, true),
spawn_link(...)

请参见此示例,并尝试其他值(与2或0取反以引发异常,以及是否使用trap_exit):
-module(play).
-compile(export_all).

start() ->
process_flag(trap_exit, true),
spawn_link(?MODULE, inverse, [2]),
loop().

loop() ->
receive
Msg -> io:format("~p~n", [Msg])
end,
loop().

inverse(N) -> 1/N.

关于erlang - Erlang和process_flag(trap_exit,true),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6720472/

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