How to reference previously started processes in an Elixir supervisor

I am starting a Supervisor that monitors two children. The second child needs a reference to the first. It seams like this should be possible because by using the one_for_rest strategy I can make sure that if the first dies the second is restarted.

children = [
  supervisor(SupervisorA, [arg1]),
  supervisor(SupervisorB, [arg2, ref_to_supervisor_a_process]),
]

supervise(children, strategy: :one_for_rest)

Ideally without having to globally name either process.


SupervisorA can supply the name: option to Supervisor.start_link/3 .

SupervisorB can then use Process.whereis/1 to resolve the name to a pid or just send messages to the named process.

https://hexdocs.pm/elixir/Supervisor.html#start_link/3 https://hexdocs.pm/elixir/Process.html#whereis/1


Supervisor.Spec.supervisor/3 returns a spec .

One might pass it through:

{id, _, _, _, _, _} = sup_a = supervisor(SupervisorA, [arg1])
children = [
  sup_a,
  supervisor(SupervisorB, [arg2, id]),
]

I recommend to use Director instead of supervisor module.
It's more flexible and poweful in restarting, removing, etc.

链接地址: http://www.djcxy.com/p/38216.html

上一篇: Erlang:单身人士的最佳途径

下一篇: 如何引用Elixir主管中以前启动的流程