[erlang-questions] How to find Pids of the children of a supervisor

Jesper Louis Andersen jesper.louis.andersen@REDACTED
Thu Dec 7 16:47:07 CET 2017

I tend to use one out of the following solutions:

* 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.
* 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.
* Use a registry: either a process, or just pick up the excellent `gproc`
system by Ulf Wiger if needed and use that.
* 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.
* 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().
* Do not underestimate having *another* process who passes the information
around. Solve stuff with an indirection!

On Thu, Dec 7, 2017 at 3:34 PM Pierre Fenoll <pierrefenoll@REDACTED> wrote:

> > supervisor:which_children/1
> > Is the the best way? - does anybody have an example?
> That and using erlang:process_info(Pid, links) are the only ways.
> The supervisor module lacks a child(SupRef, Name) function that would now
> be able to fetch children by Name
> with a complexity better than O(n) now that the module can use maps.
> I started writing a PR for that but then got confused by the code that can
> either use sets or maps: I couldn't understand which backend would be
> used when...
> My use case is to not register names for child processes (using
> erlang:register via {local,Name}).
> Cheers,
> --
> Pierre Fenoll
> On 7 December 2017 at 14:26, Vance Shipley <vances@REDACTED> wrote:
>> On Thu, Dec 7, 2017 at 6:03 PM, Joe Armstrong <erlang@REDACTED> wrote:
>> > I was asked the following in a direct mail to me:
>> >
>> > "Given two worker processes in a supervision tree, A and B, if A wants
>> > to message B, how should it obtain the pid of B ?"
>> >
>> > Pretty good question.
>> >
>> > I think the answer is to use supervisor:which_children/1 and to name
>> > the supervisor and all the children.
>> >
>> > Is the the best way? - does anybody have an example?
>> Sure, I don't bother to name the supervisor though, pass the pid() down:
>> Here's an example of a gen_server which locates it's sibling
>> simple_one_for_one supervisors after initializing:
>> https://github.com/sigscale/ocs/blob/8aafd48fd876b7434517b512d79d2c12559889bb/src/ocs_radius_auth_port_server.erl#L157
>> --
>>      -Vance
>> _______________________________________________
>> erlang-questions mailing list
>> erlang-questions@REDACTED
>> http://erlang.org/mailman/listinfo/erlang-questions
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://erlang.org/mailman/listinfo/erlang-questions
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20171207/27af58a5/attachment.htm>

More information about the erlang-questions mailing list