defmodule CounterSupervisor douse Supervisor.Behaviourdef start_link(num_counters) do:supervisor.start_link(__MODULE__, num_counters)enddef init(num_counters) doworkers = List.duplicate(worker(Counter, []))supervise(workers, strategy: :one_for_one)endend
** (EXIT from #PID<0.96.0>) {:start_spec, {:duplicate_child_name, Counter}}
workers = Enum.map(1..num_counters, fn(n) -> worker(Counter, [], [id: "counter#{n}"]) end)
--
You received this message because you are subscribed to the Google Groups "elixir-lang-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-ta...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
defmodule Counter douse GenServer.Behaviourdef start_link do:gen_server.start_link(__MODULE__, nil, [])endend
defmodule CounterSupervisor douse Supervisor.Behaviour
def start_link do:supervisor.start_link(__MODULE__, nil)enddef init(_args) doworkers = [worker(Counter, [])]supervise(workers, strategy: :simple_one_for_one)endend
iex(1)> sup = CounterSupervisor.start_link{:ok, #PID<0.47.0>}iex(2)> :supervisor.start_child(sup, [])** (exit) {{:function_clause, [{:gen, :call, [{:ok, #PID<0.47.0>}, :"$gen_call", {:start_child, []}, :infinity], [file: 'gen.erl', line: 147]}, {:gen_server, :call, 3, [file: 'gen_server.erl', line: 184]}, {:erl_eval, :do_apply, 6, [file: 'erl_eval.erl', line: 569]}, {:elixir, :eval_forms, 3, [file: 'src/elixir.erl', line: 147]}, {IEx.Server, :eval, 4, [file: '/private/tmp/elixir-lNxT/elixir-0.10.1/lib/iex/lib/iex/server.ex', line: 111]}, {IEx.Server, :wait_input, 1, [file: '/private/tmp/elixir-lNxT/elixir-0.10.1/lib/iex/lib/iex/server.ex', line: 53]}, {IEx.Server, :wait_input, 1, [file: '/private/tmp/elixir-lNxT/elixir-0.10.1/lib/iex/lib/iex/server.ex', line: 60]}, {IEx.Server, :start, 1, [file: '/private/tmp/elixir-lNxT/elixir-0.10.1/lib/iex/lib/iex/server.ex', line: 32]}]}, {:gen_server, :call, [{:ok, #PID<0.47.0>}, {:start_child, []}, :infinity]}}gen_server.erl:188: :gen_server.call/3erl_eval.erl:569: :erl_eval.do_apply/6src/elixir.erl:147: :elixir.eval_forms/3
The List.duplicate looks suspicious to me. It looks like you're supervising the same worker n times.
What happens if you instead do
workers = List.map 1..num_counters, &worker(Counter, [])
Dave
What happens if you instead do
workers = List.map 1..num_counters, &worker(Counter, [])
** (SyntaxError) /Users/paul/pb7con/Book/code/ActorsNew/word_count/lib/counter.ex:47: invalid args for &, expected an expression in the format of &Mod.fun/arity, &local/arity or a capture containing at least one argument as &1, got: worker(Counter, [])
workers = Enum.map 1..num_counters, fn(_) -> worker(Counter, []) end
Using start_link supervisor will call init/1 to find info about children and stuff. When you call start_child, it will also call this method with different arguments in order to dynamically add a child. In your case you ignore the arguments, therefore leading to twins. That's why start_link works, but it fails on start_child. See http://www.erlang.org/doc/man/supervisor.html
--You received this message because you are subscribed to a topic in the Google Groups "elixir-lang-talk" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/elixir-lang-talk/IjM0cq-cDF0/unsubscribe.
To unsubscribe from this group and all its topics, send an email to elixir-lang-ta...@googlegroups.com.
I just read the code again, I'm not sure of my answer anymore since you start a new worker... I might be wrong (happens alot).However, what I do know is that if you call start_child(sup, [ARGS]) you'll end up with init([ARGS]) in case of a simple_one_for_one. Otherwise, these ARGS contain a few specific key/values for a "childspec" (start function, etc). Check the description of "start_child" in the doc I linked
If the case of a simple_one_for_one supervisor, the child specification defined inModule:init/1 will be used and ChildSpec should instead be an arbitrary list of terms List. The child process will then be started by appending List to the existing start function arguments, i.e. by calling apply(M, F, A++List) where {M,F,A} is the start function defined in the child specification.