主管子进程与普通的spawn_link

发布于 2024-11-07 16:37:10 字数 683 浏览 6 评论 0原文

我有一个名为“monitor_node”的进程层次结构。这些monitor_nodes中的每一个都由一个supervisor来监督。

现在,这些节点中的每一个都可能具有复杂的内部结构。这意味着,它可能(或可能没有)有一些正常运行所需的子流程。示例:进程发送保活消息。到目前为止,我一直在使用普通的spawn_link来创建这些“内部” 流程。

然而,我意识到在monitor_node(正在被监督)的init函数中生成它们有时会导致该函数失败(因此整个supervisor树失败)。我的问题是:将这些内部流程附加到主管树上是一个很好的解决方案吗?我正在考虑将 Monitor_node 更改为监督其内部流程的主管。

我的疑问是:

  1. 我必须监督大量非常小的进程。我不确定这是否是一个好的做法。

  2. 我可能事先不知道给定的“内部”进程是一个简单的进程或具有一些内部结构(也会产生其他进程)。如果是后者,那么我可能应该将这些“内部-内部”进程附加到主管树。

我希望我没有让你太困惑。期待答案。

编辑:

此处讨论了一个非常相似(如果不相同)的问题 (第 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:

  1. I would have to supervise quite significant number of very small processes. I am not sure if this is a good practice.

  2. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

薄情伤 2024-11-14 16:37:11

主管:

这里有一个技巧,其中包括使用两名主管。你的树是这样的:

main_sup -> worker
main_sup -> attached_pool_sup

attached_pool_sup -> workers

主要支持是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 -> worker
main_sup -> attached_pool_sup

attached_pool_sup -> workers

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 a simple_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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文