Erlang Supervisor:如何检查是否所有工人都回复了

发布于 12-05 14:34 字数 186 浏览 1 评论 0原文

我有一个主管,有 N 个工作进程。像往常一样,主管可以向工作进程发送消息,并且有一个handle_cast将工作进程的回复发送给主管。

如何检查是否所有 N 个工人都已回复主管?是否可以通过任何类型的事件处理来实现这一点 - 即告诉主管“好的,每个人都已回复”,而不是让主管在某种 ETS 中每秒检查“所有 N 个进程已回复”状态儿童登记表?

I have a supervisor with N worker processes. As usual the supervisor can send a message to a worker process and there is a handle_cast that sends a reply from a worker to the supervisor.

How can I check that exactly all N workers have replied to the supervisor? Is it possible to implement this with any kind of event handling - i.e. to tell the supervisor "Ok, everyone has replied" and not to make the supervisor to check for the "All N processes have replied" status every second in some kind of ETS child registry table?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

骷髅2024-12-12 14:34:02

如果您谈论的是 OTP supervisor,则不可以,您无法通过它向工作人员发送消息。主管的行为非常有限,其目的是启动监控重新启动停止进程。没有别的了。

因此,为了解决您的特定问题,您必须有一个负责向所有工作人员发送消息的流程。此过程还可以保留处于其状态的所有工作人员的列表,“勾选”(或从列表中删除)已响应的工作人员。您可以通过 PID 列表来实现此目的,并接收来自进程的响应(或者通过使用 erlang:monitor/2 监视进程(如果它们在完成后退出))并查看还剩下谁。

If you are talking about an OTP supervisor, no you can't send a message to a worker from it. A supervisor is a very limited behavior with the purpose of starting, monitoring, restarting and stopping processes. Nothing else.

So to solve your particular problem, you would have to have a process that is responsible for sending a message to all workers. This process could also keep a list of all workers in its state, "tick off" (or remove from the list) the workers that have responded. You can achieve this with a list of PIDs, and receiving responses from the processes (or by monitoring the processes with erlang:monitor/2 if they're exiting when they're done) and see who's left.

鹊巢2024-12-12 14:34:02

另一种方法 - 可能(或不能)适用于您的情况是使用 gen_event 行为。

免责声明

我说“可以”,因为这取决于您的“工人”在您的具体情况下所做的事情。如果您对他们的回复内容感兴趣,您可能不想使用这种方法,但如果您只对所有工作人员完成其任务这一事实感兴趣 - 例如工作进程进行一些繁重的计算并将其部分结果存储在数据库中,以便您准备好组合部分结果 - gen_event 可能是要走的路线。

免责声明结束

所以...

在 OTP 中,事件管理器是一个可以向其发送事件的命名对象。

事件就是消息。

在事件管理器中,安装了零个、一个或多个事件处理程序。当事件管理器收到有关事件的通知时,该事件将由所有已安装的事件处理程序处理。

因此,基本上,您拥有一个事件管理器和多个事件处理程序,而不是一个主管和多个工作人员。

然后,您可以使用 gen_event:sync_notify/2 函数:

sync_notify 是同步的,因为它会在之后返回 ok
该事件已被所有事件处理程序处理。

有关 *gen_event* 的更多信息,请查看此处那里

An alternative - which could (or could not) apply to your case is to use the gen_event behaviour.

DISCLAIMER

I say "could" because it depends on what your "workers" do in your specific case. If you're interested in the content of their replies, you might prefer not to use this approach, but in the case you're interested just in the fact that all the workers completed their tasks - for example the worker processes do some heavy calculation and store their partial result on a database, so you're ready to combine the partials - the gen_event could be the route to go.

END OF DISCLAIMER

So...

In OTP, an event manager is a named object to which events can be sent.

Events are messages.

In the event manager, zero, one or several event handlers are installed. When the event manager is notified about an event, the event will be processed by all the installed event handlers.

So, basically, instead of having one supervisor and several workers, you have an event manager and several event handlers.

You could then use the gen_event:sync_notify/2 function:

sync_notify is synchronous in the sense that it will return ok after
the event has been handled by all event handlers.

For more information about the *gen_event* look here and there.

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