原创作者: hideto
阅读:1604次
评论:0条
更新时间:2011-06-01
使用BIF link将两个节点连接起来,如果其中一个节点B退出,则另一个节点A会收到{'EXIT', B, Why}的信号
可以创建一个on_exit(Pid, Fun)方法来捕获某个Pid进程的死掉:
Erlang可以远程捕获和处理错误(跨机器)
调用BIF process_flag(trap_exit, true)来将一个正常的进程转换为一个可以trap exits的系统进程
Idiom1: I Don't Care If a Process I Created Crashes
Idiom2: I Want to Die If a Process I Created Crashes
Idiom3: I Want to Handle Errors If a Process I Create Crashes
Error Handling Primitives
可以创建一个on_exit(Pid, Fun)方法来捕获某个Pid进程的死掉:
on_exit(Pid, Fun) -> spawn(fun() -> process_flag(trap_exit, true), link(Pid), receive {'EXIT', Pid, Why} -> Fun(Why) end end).
Erlang可以远程捕获和处理错误(跨机器)
调用BIF process_flag(trap_exit, true)来将一个正常的进程转换为一个可以trap exits的系统进程
trap_exit Exit Signal Action true kill Die: Broadcast the exit signal killed to the link set. true X Add {'EXIT', Pid, X} to the mailbox. false normal Continue: Do-nothing signal vanishes. false kill Die: Broadcast the exit signal killed to the link set. false X Die: Broadcast the exit signal X to the link set.
Idiom1: I Don't Care If a Process I Created Crashes
Pid = spawn(fun() -> ... end)
Idiom2: I Want to Die If a Process I Created Crashes
Pid = spawn_link(fun() -> ... end)
Idiom3: I Want to Handle Errors If a Process I Create Crashes
... process_flag(trap_exit, true), Pid = spawn_link(fun() -> ... end), ... loop(...). loop(State) -> receive {'EXIT', SomePid, Reason} -> %% do something with the error loop(State1); ... end
Error Handling Primitives
@spec spawn_link(Fun) -> Pid @spec process_flag(trap_exit, true) @spec link(Pid) -> true @spec unlink(Pid) -> true @spec exit(Why) -> none() @spec exit(Pid, Why) -> true @spec erlang:monitor(process, Item) -> MonitorRef
评论 共 0 条 请登录后发表评论