单独启动流程的监督策略

问题描述 投票:0回答:1

考虑以下基于模块的 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 启动后,如果使用 : started

另一个进程
Supervisor.start_child(MyApp.Supervisor, SomeOtherWorker)

会包含在定义的

:strategy
中吗?

process elixir erlang-otp erlang-supervisor
1个回答
0
投票

在本地测试时,单独启动的进程似乎包含在定义的

: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]}
]
© www.soinside.com 2019 - 2024. All rights reserved.