【发布时间】:2018-02-16 06:24:45
【问题描述】:
尝试通过少量测试申请Task.Supervisor.async_stream_nolink,但不起作用..
这是我的实现:
defmodule MySupervisor do
use Supervisor
def start_link do
Supervisor.start_link(__MODULE__, [])
end
def init([]) do
children= [
worker(Basic, [])
]
supervise(children, strategy: :one_for_one)
end
end
defmodule Basic do
use GenServer
def start_link do
GenServer.start_link(__MODULE__, [], name: __MODULE__)
end
end
strings = ["long string", "longer string", "there are many of these"]
stream = Task.Supervisor.async_stream_nolink(MySupervisor, strings, fn text -> text |> String.codepoints |> Enum.count end)
Enum.reduce(stream, 0, fn {:ok, num}, acc -> num + acc end) |> IO.puts
但是,我得到了错误:
** (EXIT from #PID) 退出:GenServer.call(MySupervisor, {:start_child, [#PID, :monitor, {:nonode@nohost, #PID}, {:erlang, :apply, [#Function, ["long str ring"]]}]}, :无穷) ** (EXIT) 无进程:进程不活跃或当前没有与给定名称关联的进程,可能是因为它的 应用程序未启动
16:24:34.317 [错误] GenServer #PID 终止 **(停止)退出:GenServer.call(MySupervisor, {:start_child, [#PID, :monitor, {:nonode@nohost, #PID}, {:erlang, :apply, [#Function, ["long string"]]}]}, :无穷) ** (EXIT) 无进程:进程不活跃或当前没有与给定名称关联的进程,可能是因为它的 应用程序未启动 最后一条消息:{:EXIT, #PID, {:noproc, {GenServer, :call, [MySupervisor, {:start_child, [#PID, :monitor, {:nonode@nohost, #PID}, {:erlang, :apply, [#Function
, ["long string"]]}]}, :infinity]}}} 状态:{:state, {#PID, MySupervisor}, :one_for_one, [{:child, #PID,基本,{基本, :start_link, []}, :permanent, 5000, :worker, [Basic]}], :undefined, 3, 5, [], 0, 我的主管, []}
有人可以帮忙吗?我找不到有关使用Task.Supervisor.async_stream_nolink的示例
编辑
尝试直接使用我自己的主管而不在应用程序中使用它,我尝试了这个:
{:ok, supervisor} = MySupervisor.start_link
stream = Task.Supervisor.async_stream_nolink(supervisor, strings, fn text -> text |> String.codepoints |> Enum.count end)
Enum.reduce(stream, 0, fn {:ok, num}, acc -> num + acc end) |> IO.puts
但我得到了错误:
** (EXIT from #PID) 引发异常: ** (MatchError) 右侧值不匹配:{:error, {:EXIT, {:undef, [{Basic, :start_link, [#PID, :monitor, {:nonode@nohost, #PID}, {:erlang, :apply, [#Function, ["long string"]]}], []}, {:主管,:do_start_child_i,3,[文件:'supervisor.erl',行: 381]},{:主管,:handle_call,3,[文件:'supervisor.erl',行: 406]},{:gen_server,:try_handle_call,4,[文件:'gen_server.erl', 行:615]},{:gen_server,:handle_msg,5,[文件:'gen_server.erl', 行:647]},{:proc_lib,:init_p_do_apply,3,[文件:'proc_lib.erl', 行:247]}]}}} (elixir) lib/task/supervisor.ex:265: Task.Supervisor.build_stream/5 中的匿名 fn/4 (elixir) lib/task/supervised.ex:332: Task.Supervised.stream_monitor/6
17:07:53.497 [错误] 进程 #PID 引发异常 ** (MatchError) 右侧值不匹配:{:error, {:EXIT, {:undef, [{Basic, :start_link, [#PID, :monitor, {:nonode@nohost, #PID}, {:erlang, :apply, [#Function, ["长字符串"]]}], []}, {:主管,:do_start_child_i,3,[文件:'supervisor.erl',行: 381]},{:主管,:handle_call,3,[文件:'supervisor.erl',行: 406]},{:gen_server,:try_handle_call,4,[文件:'gen_server.erl', 行:615]},{:gen_server,:handle_msg,5,[文件:'gen_server.erl', 行:647]},{:proc_lib,:init_p_do_apply,3,[文件:'proc_lib.erl', 行:247]}]}}} (elixir) lib/task/supervisor.ex:265: Task.Supervisor.build_stream/5 中的匿名 fn/4 (elixir) lib/task/supervised.ex:332: Task.Supervised.stream_monitor/6
17:07:53.503 [错误] GenServer MySupervisor 终止 ** (MatchError) 右侧值不匹配:{:error, {:EXIT, {:undef, [{Basic, :start_link, [#PID, :monitor, {:nonode@nohost, #PID}, {:erlang, :apply, [#Function, ["长字符串"]]}], []}, {:主管,:do_start_child_i,3,[文件:'supervisor.erl',行: 381]},{:主管,:handle_call,3,[文件:'supervisor.erl',行: 406]},{:gen_server,:try_handle_call,4,[文件:'gen_server.erl', 行:615]},{:gen_server,:handle_msg,5,[文件:'gen_server.erl', 行:647]},{:proc_lib,:init_p_do_apply,3,[文件:'proc_lib.erl', 行:247]}]}}} (elixir) lib/task/supervisor.ex:265: Task.Supervisor.build_stream/5 中的匿名 fn/4 (elixir) lib/task/supervised.ex:332: Task.Supervised.stream_monitor/6 最后一条消息:{:EXIT, #PID, {{:badmatch, {:error, {:EXIT, {:undef, [{Basic, :start_link, [#PID, :monitor, {:nonode@nohost, #PID}, {:erlang, :apply, [#Function, ["long string"]]}], []}, {:supervisor, :do_start_child_i, 3, [file: 'supervisor.erl', 行:381]},{:supervisor,:handle_call,3,[文件:'supervisor.erl', 行:406]},{:gen_server,:try_handle_call,4,[文件: 'gen_server.erl',行:615]},{:gen_server,:handle_msg,5,[文件: 'gen_server.erl',行:647]},{:proc_lib,:init_p_do_apply,3,[文件: 'proc_lib.erl', 行: 247]}]}}}}, [{Task.Supervisor, :"-build_str eam/5-fun-0-", 4, [file: 'lib/task/supervisor.ex', line: 265]}, {Task.Supervised, :stream_monitor, 6, [file: 'lib/task/supervised.ex', 行:332]}]}} 状态:{:state, {:local, MySupervisor}, :simple_one_for_one, [{:child, :undefined, Basic, {Basic, :start_link, []}, :permanent, 5000, :worker, [基本]}], :undefined, 3, 5, [], 0, 我的主管,[]}
17:07:53.505 [错误] GenServer #PID 终止 ** (MatchError) 右侧值不匹配:{:error, {:EXIT, {:undef, [{Basic, :start_link, [#PID, :monitor, {:nonode@nohost, #PID}, {:erlang, :apply, [#Function, ["长字符串"]]}], []}, {:主管,:do_start_child_i,3,[文件:'supervisor.erl',行: 381]},{:主管,:handle_call,3,[文件:'supervisor.erl',行: 406]},{:gen_server,:try_handle_call,4,[文件:'gen_server.erl', 行:615]},{:gen_server,:handle_msg,5,[文件:'gen_server.erl', 行:647]},{:proc_lib,:init_p_do_apply,3,[文件:'proc_lib.erl', 行:247]}]}}} (elixir) lib/task/supervisor.ex:265: Task.Supervisor.build_stream/5 中的匿名 fn/4 (elixir) lib/task/supervised.ex:332: Task.Supervised.stream_monitor/6 最后一条消息:{:EXIT, #PID, {{:badmatch, {:error, {:EXIT, {:undef, [{Basic, :start_link, [#PID, :monitor, {:nonode@nohost, #PID}, {:erlang, :apply, [#Function, ["long string"]]}], []}, {:supervisor, :do_start_child_i, 3, [file: 'supervisor.erl', 行:381]},{:supervisor,:handle_call,3,[文件:'supervisor.erl', 行:406]},{:gen_server,:try_handle_call,4,[文件: 'gen_server.erl',行:615]},{:gen_server,:handle_msg,5,[文件: 'gen_server.erl',行:647]},{:proc_lib,:init_p_do_apply,3,[文件: 'proc_lib.erl', 行: 247]}]}}}}, [{Task.Supervisor, :"-build_str eam/5-fun-0-", 4, [file: 'lib/task/supervisor.ex', line: 265]}, {Task.Supervised, :stream_monitor, 6, [file: 'lib/task/supervised.ex', 行:332]}]}} 状态:{:state, {#PID, Supervisor.Default}, :simple_one_for_one, [{:child, :undefined, Task.Supervised, {Task.Supervised, :start_link, []}, :temporary, 5000, :worker, [Task.Supervised]}], :undefined, 3 , 5, [], 0, Supervisor.Default, {:ok, {{:simple_one_for_one, 3, 5}, [{Task.Supervised, {Task.Supervised, :start_link, []}, :temporary, 5000, :worker, [Task.Supervised]}]}}}
【问题讨论】:
标签: elixir