Elixir DynamicSupervisors的工作程序名称
大家好 我对长生不老药有点陌生,对在长生不老药中为员工设置员工姓名和id很迷茫,我希望有人能帮我一点忙 我的申请文件Elixir DynamicSupervisors的工作程序名称,elixir,Elixir,大家好 我对长生不老药有点陌生,对在长生不老药中为员工设置员工姓名和id很迷茫,我希望有人能帮我一点忙 我的申请文件 defmodule Squareup.Application do # See https://hexdocs.pm/elixir/Application.html # for more information on OTP Applications @moduledoc false use Application def start(_type, _ar
defmodule Squareup.Application do
# See https://hexdocs.pm/elixir/Application.html
# for more information on OTP Applications
@moduledoc false
use Application
def start(_type, _args) do
children = [
# Starts a worker by calling: Squareup.Worker.start_link(arg)
# {Squareup.Worker, arg}
Journey,
Squareup.JourneySupervisor,
{Squareup.DynamicJourneySupervisor, strategy: :one_for_one, name: Squareup.DynamicJourneySupervisor}
]
# See https://hexdocs.pm/elixir/Supervisor.html
# for other strategies and supported options
opts = [strategy: :one_for_one, name: Squareup.Supervisor]
Supervisor.start_link(children, opts)
end
end
所以我启动了两个监管者,一个是常规监管者,一个是动态监管者:
常规的:
defmodule Squareup.JourneySupervisor do
use Supervisor
def start_link(opts) do
Supervisor.start_link(__MODULE__, opts, name: :my_test_123)
end
@impl true
def init(_init_arg) do
children = [
# Starts a worker by calling: Squareup.Worker.start_link(arg)
# {Squareup.Worker, arg}
{Queue, [:child_one]},
{Queue, [:child_two]},
]
Supervisor.init(children, strategy: :one_for_one)
end
def child_spec(opts) do
%{
id: :my_test_123s,
start: {__MODULE__, :start_link, [opts]},
shutdown: 5_000,
restart: :permanent,
type: :supervisor
}
end
end
动态的一个:
defmodule Squareup.DynamicJourneySupervisor do
use DynamicSupervisor
def start_link(init_arg) do
DynamicSupervisor.start_link(__MODULE__, init_arg, name: __MODULE__)
end
def start_child(init_args) do
# If MyWorker is not using the new child specs, we need to pass a map:
# spec = %{id: MyWorker, start: {MyWorker, :start_link, [foo, bar, baz]}}
spec = {Queue, init_args}
DynamicSupervisor.start_child(__MODULE__, spec)
end
@impl true
def init(init_arg) do
DynamicSupervisor.init(strategy: :one_for_one)
end
end
正常的管理者需要一个名为队列
defmodule Queue do
require Logger
use GenServer
### GenServer API
def init(state), do: {:ok, state}
def handle_call(:dequeue, _from, [value | state]) do {:reply, value, state} end
def handle_call(:dequeue, _from, []), do: {:reply, nil, []}
def handle_call(:queue, _from, state), do: {:reply, state, state}
def handle_cast({:enqueue, value}, state) do {:noreply, state ++ [value]} end
### Client API / Helper functions
def start_link(state \\ []) do
Logger.info("Initializing queue with state:")
Logger.info(inspect(state))
GenServer.start_link(__MODULE__, state, name: List.first(state))
end
def child_spec(opts) do
Logger.info("Initializing child_spec:")
Logger.info(inspect(opts))
%{
id: List.first(opts),
start: {__MODULE__, :start_link, [opts]},
shutdown: 5_000,
restart: :permanent,
type: :worker
}
end
def queue, do: GenServer.call(__MODULE__, :queue)
def enqueue(value), do: GenServer.cast(__MODULE__, {:enqueue, value})
def dequeue, do: GenServer.call(__MODULE__, :dequeue)
end
启动应用程序时,我将执行以下操作:
12:55:45.744 [info] Initializing child_spec:
12:55:45.749 [info] [:child_one]
12:55:45.749 [info] Initializing child_spec:
12:55:45.749 [info] [:child_two]
12:55:45.749 [info] Initializing queue with state:
12:55:45.749 [info] [:child_one]
12:55:45.749 [info] Initializing queue with state:
12:55:45.749 [info] [:child_two]
iex(4)> Supervisor.which_children(:my_test_123)
[
{:child_two, #PID<0.218.0>, :worker, [Queue]},
{:child_one, #PID<0.217.0>, :worker, [Queue]}
]
并查找工作人员名称,则它们始终是:未定义的
iex(5)> Supervisor.which_children(Squareup.DynamicJourneySupervisor)
[{:undefined, #PID<0.224.0>, :worker, [Queue]}]
iex(5)>主管。哪个孩子(Squareup.DynamicJourneySupervisor)
[{:undefined,#PID,:worker,[Queue]}]
工人的id似乎设置正确。在通过DynamicSupervisor启动进程时,是否有方法设置名称
已经提前多谢了
Leon您似乎误解了哪个子函数/1
返回的值。它返回一个包含所有子对象信息的列表
从:
因此,您实际上正确地命名了流程。只有队列
模块的客户端代码不正确。如果您想访问命名的GenServer,您应该修复函数queue
enqueue
和dequeue
,以实际调用命名的GenServer
我的意思是,它们应该是这样的,例如:
def queue(name \\ __MODULE__), do: GenServer.call(name, :queue)
def enqueue(name \\ __MODULE__, value), do: GenServer.cast(name, {:enqueue, value})
def dequeue(name \\ __MODULE__), do: GenServer.call(name, :dequeue)
这样,GenServer.call/2
将始终调用命名的GenServer。您还可以通过其PID访问它们,而不是通过名称访问它们(如果需要,您可以从接收这些PID,而的子项/1
会动态运行)。是的,您说得对。它确实只是在文档中。非常感谢,也非常感谢队列模块修复!
* id - it is always :undefined for dynamic supervisors
def queue(name \\ __MODULE__), do: GenServer.call(name, :queue)
def enqueue(name \\ __MODULE__, value), do: GenServer.cast(name, {:enqueue, value})
def dequeue(name \\ __MODULE__), do: GenServer.call(name, :dequeue)