[erlang-questions] supervisor:start_child
Vance Shipley
vances@REDACTED
Mon May 2 22:56:38 CEST 2011
On Mon, May 02, 2011 at 11:19:59PM +0300, Martin Dimitrov wrote:
} In our app we actually use the sup functions (delete_child and so on)
} but we keep only the pids and since the relationship is one-to-one
But really the relationship is not one to one but one to many,
at least over time. Consider this:
Start a supervisor with one child:
1> {ok, Sup} = supervisor:start_link(super, []).
{ok,<0.33.0>}
Start a new child:
2> Id = make_ref().
#Ref<0.0.0.32>
3> ChildSpec = {Id, {gen_server, start_link, [server, [], []]}, permanent, 4000, worker, [server]}.
{#Ref<0.0.0.32>,
{gen_server,start_link,[server,[],[]]},
permanent,4000,worker,
[server]}
4> supervisor:start_child(Sup, ChildSpec).
{ok,<0.38.0>}
Now we have a process with pid() <0.38.0> created by the child specification:
5> Children = supervisor:which_children(Sup).
[{#Ref<0.0.0.32>,<0.38.0>,worker,[server]},
{server,<0.34.0>,worker,[server]}]
Now something tragic happens to that child:
6> {Id, Pid, _, _} = lists:keyfind(Id, 1, Children).
{#Ref<0.0.0.32>,<0.38.0>,worker,[server]}
7> exit(Pid, oops).
true
That child specification is still identified by the unique reference
however the process it is managing has changed to <0.42.0>:
8> supervisor:which_children(Sup).
[{#Ref<0.0.0.32>,<0.42.0>,worker,[server]},
{server,<0.34.0>,worker,[server]}]
} we retrieve the dynamic id. I thought that there might be an easier
} way but from what I understand now there isn't.
You either need to save the Id or dynamically learn it:
9> {Id, Pid, _, _} = lists:keyfind(Pid, 2, Children).
{#Ref<0.0.0.32>,<0.38.0>,worker,[server]}
I would add though that it is likely you really be considering using
another supervision layer. Start a simple_one_for_one supervisor
for each type of worker under the supervisor you are using now.
--
-Vance
More information about the erlang-questions
mailing list