2008-01-14
Programming Erlang读书笔记9: 并行程序里的错误
关键字: error handle
使用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
发表评论
- 浏览: 681850 次
- 性别:

- 来自: BJ

- 详细资料
搜索本博客
我的相册
screenshot
共 1 张
共 1 张
最近加入圈子
最新评论
-
Mnesia用户手册:三,构建 ...
要想创建disc_copies和disc_only_copies类型的表有两个前 ...
-- by hideto -
翻译www.djangobook.com之 ...
有个问题问一下: 我先配置了一个urlpatterns是这样的: r'^myd ...
-- by lyhapple -
Why OO sucks
gigix 写道lyl0035 写道为啥就没人想想,其实在面向对象的代码中也流露 ...
-- by hurd -
Why OO sucks
貌似又回到当年java vs c的年代。两种方式,不管是OO还是FP,仅是人处理 ...
-- by python -
大家可以抛弃Java踹死Djan ...
to phoenixup:1,你还别说,你举的什么Struts,Tapestry ...
-- by hideto






评论排行榜