multiple children for a supervisor

knotito Source

I am a totally beginner in elixir, then I tried (inspired by the examples of programming elixir book) to create a supervisor that can have several children of the same type. I got the error

{:error, {{:EXIT, {{:badmatch, {:error,  {:already_started,#PID<0.122.0>}}},...

Maybe I misunderstand the usage of a supervisor and have to create a supervisor for each process.

Can anyone explain me the right usage of supervisors in this case ?

erlangelixirotp

Answers

answered 1 year ago Roger Lipscombe #1

(I don't do Elixir, so this all uses Erlang syntax)

create a supervisor that can have several children of the same type

Supervisors don't care about the "type" (in the sense you're using it) of the children. The child spec contains an id, a start function, and a few other things which aren't relevant to your question.

Provided the id is different, you can use the same start function for multiple children.

Where I suspect you're having difficulty is that your child has a name. That is: in your init function, you've called gen_server:start_link/4, passing something like {local, ?MODULE}. These processes all attempt to register with the same name, so you get the already_started error.

comments powered by Disqus