考虑以下基于模块的 Supervisor:
defmodule MyApp.Supervisor do
use Supervisor
def start_link(init_arg) do
Supervisor.start_link(__MODULE__, init_arg, name: __MODULE__)
end
@impl true
def init(_init_arg) do
Supervisor.init([Worker], strategy: :rest_for_one)
end
end
另一个进程
Supervisor.start_child(MyApp.Supervisor, SomeOtherWorker)
会包含在定义的
:strategy
中吗?
在本地测试时,单独启动的进程似乎包含在定义的
:strategy
中:
Erlang/OTP 26 [erts-14.0.2] [source] [64-bit] [smp:4:4] [ds:4:4:10] [async-threads:1] [jit:ns]
Interactive Elixir (1.15.4) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)> Supervisor.which_children(SupervisorDemo.Supervisor)
[{Worker, #PID<0.140.0>, :worker, [Worker]}]
iex(2)> Supervisor.start_child(SupervisorDemo.Supervisor, SomeOtherWorker)
{:ok, #PID<0.156.0>}
iex(3)> Supervisor.which_children(SupervisorDemo.Supervisor)
[
{SomeOtherWorker, #PID<0.156.0>, :worker, [SomeOtherWorker]},
{Worker, #PID<0.140.0>, :worker, [Worker]}
iex(4)> GenServer.stop(Worker)
:ok
iex(5)> Supervisor.which_children(SupervisorDemo.Supervisor)
[
{SomeOtherWorker, #PID<0.158.0>, :worker, [SomeOtherWorker]},
{Worker, #PID<0.157.0>, :worker, [Worker]}
]