主管子进程与普通的spawn_link
我有一个名为“monitor_node”的进程层次结构。这些monitor_nodes中的每一个都由一个supervisor来监督。
现在,这些节点中的每一个都可能具有复杂的内部结构。这意味着,它可能(或可能没有)有一些正常运行所需的子流程。示例:进程发送保活消息。到目前为止,我一直在使用普通的spawn_link来创建这些“内部” 流程。
然而,我意识到在monitor_node(正在被监督)的init函数中生成它们有时会导致该函数失败(因此整个supervisor树失败)。我的问题是:将这些内部流程附加到主管树上是一个很好的解决方案吗?我正在考虑将 Monitor_node 更改为监督其内部流程的主管。
我的疑问是:
我必须监督大量非常小的进程。我不确定这是否是一个好的做法。
我可能事先不知道给定的“内部”进程是一个简单的进程或具有一些内部结构(也会产生其他进程)。如果是后者,那么我可能应该将这些“内部-内部”进程附加到主管树。
我希望我没有让你太困惑。期待答案。
编辑:
此处讨论了一个非常相似(如果不相同)的问题 (第 3 篇文章)。给出的解决方案与我给出的垃圾答案几乎相同。
I have a hierarchy of processes called "monitor_node". Each of these monitor_nodes is supervised by one supervisor.
Now, each of these nodes may have a complex inner structure. Meaning, it may (or may not) have some subprocesses that are needed for it to operate properly. Example: process sending keep-alive messages. So far I have been using plain spawn_link to create these "internal"
processes.
However, I have realized that spawning them in init function of monitor_node (which is being supervised) sometimes causes this function to fail (and therefore whole supervisor tree fails). My question is: would it be a good solution to attach these internal processes to supervisor tree? I am thinking about changing monitor_node to a supervisor that supervises it's internal processes.
My doubts are:
I would have to supervise quite significant number of very small processes. I am not sure if this is a good practice.
I may not know in advance that given "internal" process is a simple process or has some internal structure (also spawns other processes). If the latter is the case then I probably should attach these "inner-inner" processes to the supervisor tree.
I hope I have not confused you too much. Looking forward for an answer.
EDIT:
A very similar (if not the same) problem is discusses here (3rd post). The solution given is pretty much the same as the one that I GIVE CRAP ANSWERS gave.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
主管:
这里有一个技巧,其中包括使用两名主管。你的树是这样的:
主要支持是
one_for_all
,所以如果工人或池主管死亡,那么树就完成并被杀死。池主管是一个simple_one_for_one
,适合拥有数百或数千名工人。Init:
不要在 init 回调中做太多工作。主管将等待初始化完成,如果花费的时间比正常情况长,您可以设置超时(根据您的情况可以增加超时时间)。
一个技巧是快速超时(从 init 中返回超时 0),然后在
handle_info
超时回调中处理其他设置。这样你就不会妨碍主要主管了。小心这里的比赛!Supervisors:
There is a trick here, which includes the use of two supervisors. Your tree goes like:
main sup is
one_for_all
, so if the worker or the pool supervisor dies, then the tree is done for and killed off. The pool supervisor is asimple_one_for_one
which are suitable for having hundreds or thousands of workers.Init:
Don't do too much work in your init callback. The supervisor will wait until the init completes and you can set a timeout (which you can increase in your case) if it takes longer than normal.
A trick is to quickly timeout (return with a timeout of 0 from init) and then handle additional setup in the
handle_info
timeout callback. That way you won't be stopping up the main supervisor. Beware of races here!