<div dir="ltr"><div><div><div><div><div><div>I tend to use one out of the following solutions:<br><br></div>* If the processes are truly "singletons" then I tend to register them in the registry (locally or globally) and then look them up there by name.<br></div>* Sometimes, A spawns B in the same supervisor. In that case, you obtain B's pid() as part of the spawn and can store it. Of course, if B goes away, so does A need to so you need to handle crashing in the right way here.<br></div>* Use a registry: either a process, or just pick up the excellent `gproc` system by Ulf Wiger if needed and use that.<br></div>* In a distributed system, I think there is enough interesting stuff going on right now that I'd resurvey the area and pick a registry with the guarantees I need in the system.<br></div>* Some of my old code used `supervisor:which_children()` but the more experience I have, the more often I tend to use a registry application for these kinds of things. Keeping a manager process, or an ETS table (gproc) is usually an easier way to obtain other pids().<br></div>* Do not underestimate having *another* process who passes the information around. Solve stuff with an indirection!<br><br></div><br><div class="gmail_quote"><div dir="ltr">On Thu, Dec 7, 2017 at 3:34 PM Pierre Fenoll <<a href="mailto:pierrefenoll@gmail.com">pierrefenoll@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">> <span style="font-size:12.8px">supervisor:which_children/1</span></div><div dir="ltr"><div><span style="font-size:12.8px">> Is the the best way? - does anybody have an example?</span></div><div><span style="font-size:12.8px"><br></span></div></div><div dir="ltr"><div><span style="font-size:12.8px">That and using erlang:process_info(Pid, links) are the only ways.</span></div><div><span style="font-size:12.8px"><br></span></div><div><span style="font-size:12.8px">The supervisor module lacks a child(SupRef, Name) function that would now be able to fetch children by Name</span></div><div><span style="font-size:12.8px">with a complexity better than O(n) now that the module can use maps.</span></div><div><span style="font-size:12.8px">I started writing a PR for that but then got confused by the code that can either use sets or maps: </span><span style="font-size:12.8px">I couldn't understand which backend would be used when...</span></div><div><span style="font-size:12.8px"><br></span></div><div><span style="font-size:12.8px">My use case is to not register names for child processes (using erlang:register via {local,Name}).</span></div><div><span style="font-size:12.8px"><br></span></div></div><div class="gmail_extra"><br clear="all"><div><div class="m_-3633894252371836013gmail_signature" data-smartmail="gmail_signature"><div dir="ltr"><div><div><br></div><div>Cheers,</div></div></div></div></div></div><div class="gmail_extra"><div><div class="m_-3633894252371836013gmail_signature" data-smartmail="gmail_signature"><div dir="ltr"><div><div>-- </div><div>Pierre Fenoll</div></div></div></div></div></div><div class="gmail_extra">
<br><div class="gmail_quote">On 7 December 2017 at 14:26, Vance Shipley <span dir="ltr"><<a href="mailto:vances@motivity.ca" target="_blank">vances@motivity.ca</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span>On Thu, Dec 7, 2017 at 6:03 PM, Joe Armstrong <<a href="mailto:erlang@gmail.com" target="_blank">erlang@gmail.com</a>> wrote:<br>
> I was asked the following in a direct mail to me:<br>
><br>
> "Given two worker processes in a supervision tree, A and B, if A wants<br>
> to message B, how should it obtain the pid of B ?"<br>
><br>
> Pretty good question.<br>
><br>
> I think the answer is to use supervisor:which_children/1 and to name<br>
> the supervisor and all the children.<br>
><br>
> Is the the best way? - does anybody have an example?<br>
<br>
</span>Sure, I don't bother to name the supervisor though, pass the pid() down:<br>
<br>
Here's an example of a gen_server which locates it's sibling<br>
simple_one_for_one supervisors after initializing:<br>
<a href="https://github.com/sigscale/ocs/blob/8aafd48fd876b7434517b512d79d2c12559889bb/src/ocs_radius_auth_port_server.erl#L157" rel="noreferrer" target="_blank">https://github.com/sigscale/ocs/blob/8aafd48fd876b7434517b512d79d2c12559889bb/src/ocs_radius_auth_port_server.erl#L157</a><br>
<span class="m_-3633894252371836013HOEnZb"><font color="#888888"><br>
<br>
<br>
--<br>
     -Vance<br>
</font></span><div class="m_-3633894252371836013HOEnZb"><div class="m_-3633894252371836013h5">_______________________________________________<br>
erlang-questions mailing list<br>
<a href="mailto:erlang-questions@erlang.org" target="_blank">erlang-questions@erlang.org</a><br>
<a href="http://erlang.org/mailman/listinfo/erlang-questions" rel="noreferrer" target="_blank">http://erlang.org/mailman/listinfo/erlang-questions</a><br>
</div></div></blockquote></div><br></div>
_______________________________________________<br>
erlang-questions mailing list<br>
<a href="mailto:erlang-questions@erlang.org" target="_blank">erlang-questions@erlang.org</a><br>
<a href="http://erlang.org/mailman/listinfo/erlang-questions" rel="noreferrer" target="_blank">http://erlang.org/mailman/listinfo/erlang-questions</a><br>
</blockquote></div>