监督重新启动进程的子进程
我有一个像这样的结构
-------------
|Supervisor |
-------------
|
-------------
| Child1 |
-------------
|
-------------
| Child2 |
-------------
在这个结构中,child1 受到监督并生成 child2。我需要的是当 child2 崩溃/退出时能够重新启动 child1。哪种方法是实现这一目标的最佳方法?
I have a structure like this
-------------
|Supervisor |
-------------
|
-------------
| Child1 |
-------------
|
-------------
| Child2 |
-------------
In this structure, child1 is supervised and it spawns child2. What I need is to be able to restart child1 when child2 crashes/exits. Which would be the best way to achieve this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您允许
Child1
在Child2
崩溃时崩溃,您现有的主管将简单地重新启动
Child1
,从而也会重新启动Child2
。但这取决于
Child1
在Child2
崩溃时也崩溃。另一种选择是在进程树中插入另一个主管:
新的主管仅将两个子进程作为自己的服务处理,从而允许
一个人的死亡会以可配置的方式影响另一个人。
If you allow
Child1
to crash whenChild2
crashes, your existing supervisorwill simply restart
Child1
, thus also restartingChild2
.But that depends upon
Child1
crashing whenChild2
crashes. Another optionis to insert another supervisor in the process tree:
The new supervisor handles just the two children as their own service, allowing
the death of either one to influence the other in configurable ways.
查看 Erlang 文档:http://www.erlang.org/doc/design_principles/sup_princ。 html
您所要做的就是重新排列进程树并使用
one_for_all
重新启动策略。在我看来 Child1 和 Child2 应该是 Supervisor 的孩子。或者,如果您想保持当前状态,则必须在进程 Child1 中捕获进程 Child2 的 EXIT。当 EXIT 到达 Child1 时,所有 Child1 必须返回的是:
并且它将由 Supervisor 自动重新启动。 Supervisor 必须处于
永久
重启模式。Check out Erlang documentation: http://www.erlang.org/doc/design_principles/sup_princ.html
All you have to do is to rearrange you process-tree and use
one_for_all
restart strategy. In my oppinion Child1 and Child2 should be children of Supervisor.Or, in case you want to keep things like they are at the moment you have to catch EXIT of process Child2 in process Child1. When EXIT comes to Child1 all Child1 has to return is:
and it will be automaticall restarted by Supervisor. Supervisor must be in
permanent
restart mode.