DynamicSupervisor
A supervisor in Elixir starts the children it was given when it starts, and the list is fixed. A DynamicSupervisor starts with no children and accepts new ones while it runs, which is the tool for a listener, a connection or a job whose number is not known at boot.
A child at run time
DynamicSupervisor.start_link/1 takes one strategy and no children. start_child/2 takes a child specification, starts the child under the supervisor and answers its pid:
dynamic.exs
{:ok, sup} = DynamicSupervisor.start_link(strategy: :one_for_one) {:ok, pid} = DynamicSupervisor.start_child(sup, {Task, fn -> Process.sleep(:infinity) end}) IO.inspect(DynamicSupervisor.count_children(sup)) IO.inspect(is_pid(pid))
elixir dynamic.exs
%{active: 1, workers: 1, supervisors: 0, specs: 1}
true
The {Task, fun} form is a child specification built from the Task module, and it is the shortest way to start a process under supervision. The child is not linked to the caller; its fate belongs to the supervisor, which is what makes it different from spawn/1.
Many children of one kind
Each call adds one child, and the supervisor keeps them apart without ids from the caller. A program that starts a listener per connection calls this function with the same specification and keeps the pid the call answers:
many.exs
{:ok, sup} = DynamicSupervisor.start_link(strategy: :one_for_one) children = for _ <- 1..3 do {:ok, pid} = DynamicSupervisor.start_child(sup, {Task, fn -> Process.sleep(:infinity) end}) pid end IO.inspect(length(children)) IO.inspect(DynamicSupervisor.count_children(sup)) IO.inspect(length(DynamicSupervisor.which_children(sup)))
elixir many.exs
3
%{active: 3, workers: 3, supervisors: 0, specs: 3}
3
The three lines say the same thing from three angles: the pids the caller kept, the report of the supervisor and the list of children. A dynamic supervisor has no upper limit of its own, so a program that accepts connections needs a limit of its own, such as a counter in the accept loop.
The permanent child
A child specification carries a restart policy. The default is :permanent, so a child that dies is started again from its specification:
restart.exs
defmodule Worker do use Agent def start_link(_opts), do: Agent.start_link(fn -> 0 end) def child_spec(_opts) do %{id: __MODULE__, start: {__MODULE__, :start_link, [[]]}, restart: :permanent} end end {:ok, sup} = DynamicSupervisor.start_link(strategy: :one_for_one) {:ok, pid} = DynamicSupervisor.start_child(sup, Worker) Process.exit(pid, :kill) Process.sleep(50) [{_, new_pid, _, _}] = DynamicSupervisor.which_children(sup) IO.inspect(pid != new_pid) IO.inspect(DynamicSupervisor.count_children(sup))
elixir restart.exs
true
%{active: 1, workers: 1, supervisors: 0, specs: 1}
The pid changed and the count stayed at one. A child with restart: :temporary is not started again, which is the policy for a job whose result the caller already has; the restart strategies lesson compares the three policies.
Stopping one child
DynamicSupervisor.terminate_child/2 stops one child and does not restart it, because the supervisor treats the stop as a request rather than a crash. The count of the children goes down by one:
stop.exs
{:ok, sup} = DynamicSupervisor.start_link(strategy: :one_for_one) {:ok, pid} = DynamicSupervisor.start_child(sup, {Task, fn -> Process.sleep(:infinity) end}) IO.inspect(DynamicSupervisor.terminate_child(sup, pid)) IO.inspect(DynamicSupervisor.count_children(sup))
elixir stop.exs
:ok
%{active: 0, workers: 0, supervisors: 0, specs: 0}
The specification is removed with the child, which is the difference from a stopped child of a fixed supervisor: that one keeps its spec and is started again. A program that closes a connection simply terminates the child, and the supervisor forgets it.
The mistake: a pid that is not a child
terminate_child/2 only knows the children of its own supervisor. A pid from spawn/1, a child of another supervisor or the caller itself answers {:error, :not_found}:
notfound.exs
{:ok, sup} = DynamicSupervisor.start_link(strategy: :one_for_one) IO.inspect(DynamicSupervisor.terminate_child(sup, self()))
elixir notfound.exs
{:error, :not_found}
The caller is alive and the supervisor did not touch it. A program that stops a child should keep the pid that start_child/2 answered and pass that one; a pid from anywhere else is the wrong handle for this supervisor.
The programs on this page were run with Elixir 1.19.7 on Erlang/OTP 27 (erts 15.2.7.14), on Linux x86_64 in a Docker container.
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What does a
DynamicSupervisorstart with?Answer
No children; they are added with start_child/2
A dynamic supervisor starts empty and accepts children while it runs.
-
What does
DynamicSupervisor.start_child/2answer?Answer
{:ok, pid} or an error tuple
The call answers the result of the start, which is {:ok, pid} on success.
-
Why does a dynamic supervisor suit a listener per connection?
Answer
Because the number of listeners is not known at boot
Children come and go at run time; a fixed supervisor list cannot express that.
-
What does
terminate_child/2do to the child specification?Answer
It removes the specification with the child
A dynamic supervisor forgets a child that was terminated on request.
-
What does this program print?
{:ok, sup} = DynamicSupervisor.start_link(strategy: :one_for_one) {:ok, _pid} = DynamicSupervisor.start_child(sup, {Task, fn -> Process.sleep(:infinity) end}) IO.inspect(DynamicSupervisor.count_children(sup))Answer
%{active: 1, workers: 1, supervisors: 0, specs: 1}
One task child is active and its specification exists.
-
What does this program print?
{:ok, sup} = DynamicSupervisor.start_link(strategy: :one_for_one) pids = for _ <- 1..2 do {:ok, pid} = DynamicSupervisor.start_child(sup, {Task, fn -> Process.sleep(:infinity) end}) pid end IO.puts(length(pids))Answer
2
Each start adds one child, and both pids are kept in the list.
-
What does this program print?
{:ok, sup} = DynamicSupervisor.start_link(strategy: :one_for_one) {:ok, pid} = DynamicSupervisor.start_child(sup, {Task, fn -> Process.sleep(:infinity) end}) IO.inspect(DynamicSupervisor.terminate_child(sup, pid)) IO.inspect(DynamicSupervisor.count_children(sup))Answer
:ok then %{active: 0, workers: 0, supervisors: 0, specs: 0}
The child is terminated and its spec is removed, so no child stays.
-
What does this program print?
{:ok, sup} = DynamicSupervisor.start_link(strategy: :one_for_one) IO.inspect(DynamicSupervisor.terminate_child(sup, self()))Answer
{:error, :not_found}
The caller is not a child of this supervisor, so the call finds nothing to terminate.
-
A child of a dynamic supervisor has
restart: :temporary. What happens when it ends?Answer
It stays ended and is removed
A temporary child is not restarted; the supervisor removes the finished child.
-
How does a fixed supervisor differ from a dynamic one?
Answer
A fixed one starts a list given at start; a dynamic one starts children later
The fixed list is part of the start; a dynamic supervisor adds children through start_child/2.
Автор статьи: Arthur Isaev