At first glance* DynamicSupervisor *appears to be be fully compatible with
*Supervisor*, as it uses exactly the same method for specifying children
h DynamicSupervisor.start_child
> *child_spec should be a valid child specification as detailed in the*
> *"child_spec/1" section of the documentation for Supervisor. The child
> process**will be started as defined in the child specification.*
However, during tests (elixir 1.8.1), I noticed that *DynamicSupervisor* allows
registering multiple children with the same id
{:ok, ds} = DynamicSupervisor.start_link(strategy: :one_for_one)
child_spec = Supervisor.child_spec({Agent, fn -> :ok end}, *id: :child*,
type: :worker)
Enum.each(0..5, fn _ -> {:ok, _pid} = DynamicSupervisor.start_child(ds,
child_spec) end)
:ok
the code above succeeds, but the same wouldn't be possible with *Supervisor*
{:ok, s} = Supervisor.start_link([], strategy: :one_for_one)
child_spec = Supervisor.child_spec({Agent, fn -> :ok end}, *id: :child*,
type: :worker)
Enum.each(0..5, fn _ -> {:ok, _pid} = Supervisor.start_child(s, child_spec)
end)
** (MatchError) no match of right hand side value: {:error,
> {:already_started, #PID<0.2977.0>}}
> (stdlib) erl_eval.erl:453: :erl_eval.expr/5
> (elixir) lib/enum.ex:775: anonymous fn/3 in Enum.each/2
> (elixir) lib/enum.ex:3003: Enum.reduce_range_inc/4
> (elixir) lib/enum.ex:1945: Enum.each/2
Moreover if you run *DynamicSupervisor.which_children/1*, you can see that
ids are not only unchecked, but are not even preserved:
DynamicSupervisor.which_children(ds)
[
> {*:undefined*, #PID<0.2948.0>, :worker, [Agent]},
> {*:undefined*, #PID<0.2949.0>, :worker, [Agent]},
> {*:undefined*, #PID<0.2950.0>, :worker, [Agent]},
> {*:undefined*, #PID<0.2951.0>, :worker, [Agent]},
> {*:undefined*, #PID<0.2952.0>, :worker, [Agent]},
> {*:undefined*, #PID<0.2953.0>, :worker, [Agent]}
> ]
I think it would be beneficial if the documentation of *DynamicSupervisor
*would
be explicit about this incompatibility. Currently the documentation gives
impression that behaviorally they are almost the same (except for the
ability to easily impose a limit on the number of children).
--
You received this message because you are subscribed to the Google Groups
"elixir-lang-core" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/elixir-lang-core/347e25a6-6b7d-4d7e-ac01-987490661a19%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.