【发布时间】:2019-03-08 06:04:27
【问题描述】:
我正在尝试从用户那里获取输入,然后创建 genservers 的数量作为输入并监督它们。我的代码类似于
defmodule GossSim do
use Supervisor
def main(args) do
# Since it is a mix generated project I had to put the main
Supervisor.start_link(__MODULE__, args)
end
def start_link(args) do
Supervisor.start_link(__MODULE__, args)
end
#The two methods down below create children dynamically
def get_child_list(child_list, 0), do: child_list
def get_child_list(child_list, num_of_nodes) do
child =
%{
id: :rand.uniform(num_of_nodes*100000),
start: {Gossip_node, :start_link, [[0,true]]}
}
if num_of_nodes > 0 do
get_child_list( [child] ++ child_list, num_of_nodes-1)
end
end
def init(args) do
children = get_child_list([], 10)
# The outut of this inspect is pasted below
IO.inspect children, label: "The children list is"
// some function that does something parse_args_and_start(args)
// num_of_nodes is 10
children = get_child_list([], num_of_nodes)
Supervisor.init(children, strategy: :simple_one_for_one)
end
我收到以下错误
bad child specification, invalid children: [%{id: 512, start: {Gossip_node, :start_link, [[0, true]]}, type: :worker}, %{id: 49677, start: {Gossip_node, :start_link, [[0, true]]}, type: :worker},
后面是所有子进程的列表。进程有不同的ID
主管文档说,如果主管有一个开始和一个 ID,它就很好。由于孩子是一个列表,我在其中添加了多个孩子的地图。我是不是错过了什么。 Gossip_node 是同一文件夹中的一个模块。
【问题讨论】:
标签: elixir erlang-supervisor gen-server