如何广播到所有链接的进程?

时间:2014-04-11 08:41:17

标签: process erlang publish-subscribe broadcast

我们说我有一个过程X,他与其他3个过程[U,Y,Z]相关联。如何从X向这些进程广播消息?

2 个答案:

答案 0 :(得分:4)

您可以找到与erlang相关的链接:process_info / 2,如

[Pid ! Msg || Pid <- element(2, process_info(self(), links))]

答案 1 :(得分:3)

检查process_info/2文档:

broadcast_to_linked(Message) ->
  {links, Links} = process_info(self(), links),
  Fun = fun(P) -> P ! Message end,
  lists:foreach(Fun, Links).