Supervisor child pid

Roger Lipscombe roger@REDACTED
Tue Mar 3 21:54:36 CET 2020


supervisor:get_child_spec/2 seems less brittle and more understandable.

On Tue, 3 Mar 2020 at 19:51, Zsolt Laky <zsoci@REDACTED> wrote:
>
> Dear Team,
>
> Finding a child Pid under a supervisor can be costly with the suggestion I found on the net with supervisor:which_children/1 as with a high number of children it returns a huge list with {Id, Child, Type, Modules} to find the MyChildId in.
>
> What I did is:
>
> -spec lookup(Name) -> Result when
>  Name   :: term(),
>  Result :: {ok, pid()} | {error, not_registered}.
> lookup(Name) ->
>  FakeChild = #{ id => Name,
>                 start => {?MODULE, ignore_start_child, []},
>                 restart => temporary,
>                 shutdown => 2000,
>                 type => worker,
>                 modules => dynamic},
>  case supervisor:start_child(actor_sup, FakeChild) of
>    {error, {already_started, Pid}} ->
>      {ok, Pid};
>    _Else -> %% {error, {ignored, _}}
>      {error, not_registered}
>  end.
>
> -spec ignore_start_child() ->
>  {error, ignored}.
> ignore_start_child() ->
>  {error, ignored}.
>
> I ask the experts if they see any pitfall I might have missed.
>
> Thanks in advance and kind regards,
> Zsolt


More information about the erlang-questions mailing list