Supervisor child pid

Zsolt Laky zsoci@REDACTED
Tue Mar 3 20:08:00 CET 2020


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