尝试在很少测试的情况下应用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
但是,我收到了错误:
**(退出#PID< 0.70.0>退出):GenServer.call(MySupervisor,{:start_child,[#PID< 0.70.0>,:monitor, {:nonode @ nohost,#PID< 0.81.0>},{:erlang,:apply, [#Function< 0.118488666 in file:test.exs>,[" long st ring"]]}]}, :无穷) **(退出)无流程:流程尚未生效或当前没有与给定名称关联的流程,可能是因为 申请尚未开始
16:24:34.317 [错误] GenServer#PID< 0.79.0>终止 **(停止)退出:GenServer.call(MySupervisor,{:start_child,[#PID< 0.70.0>,:monitor,{:nonode @ nohost,#PID< 0.81.0>},{:erlang, :apply,[#Function< 0.118488666 in file:test.exs>,[" long string"]]}]}, :无穷) **(退出)无流程:流程尚未生效或当前没有与给定名称关联的流程,可能是因为 应用程序尚未启动上一条消息:{:EXIT,#PID< 0.70.0>, {:noproc,{GenServer,:call,[MySupervisor,{:start_child, [#PID< 0.70.0>,:monitor,{:nonode @ nohost,#PID< 0.81.0>},{:erlang, :apply,[#Function< 0.118488666 in file:test.exs
,[" long string"]]}]},:infinity]}}}状态:{:state,{#PID< 0.79.0>,MySupervisor},:one_for_one,[{: child,#PID< 0.80.0&gt ;, Basic,{Basic, :start_link,[]} ,: permanent,5000,:worker,[Basic]}],:undefined,3, 5,[],0,MySupervisor,[]}
任何人都可以提供帮助?我找不到关于使用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
但我收到了错误:
**(从#PID< 0.70.0>退出)引发了异常: **(MatchError)右侧值不匹配:{:error,{:EXIT,{:undef,[{Basic,:start_link,[#PID< 0.70.0>,:monitor, {:nonode @ nohost,#PID< 0.81.0>},{:erlang,:apply, [#Function< 0.20003547 in file:t est.exs>,[" long string"]]}],[]}, {:主管,:do_start_child_i,3,[文件:' supervisor.erl',行: 381]},{:supervisor,:handle_call,3,[file:' supervisor.erl',line: 406]},{:gen_server,:try_handle_call,4,[file:' gen_server.erl', line:615]},{:gen_server,:handle_msg,5,[file:' gen_server.erl', line:647]},{:proc_lib,:init_p_do_apply,3,[file:' proc_lib.erl', line: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< 0.81.0>提出异常 **(MatchError)右侧值不匹配:{:error,{:EXIT,{:undef,[{Basic,:start_link,[#PID< 0.70.0>,:monitor, {:nonode @ nohost,#PID< 0.81.0>},{:erlang,:apply, [#Function< 0.20003547 in file:test。 exs>,[" long string"]]}],[]}, {:主管,:do_start_child_i,3,[文件:' supervisor.erl',行: 381]},{:supervisor,:handle_call,3,[file:' supervisor.erl',line: 406]},{:gen_server,:try_handle_call,4,[file:' gen_server.erl', line:615]},{:gen_server,:handle_msg,5,[file:' gen_server.erl', line:647]},{:proc_lib,:init_p_do_apply,3,[file:' proc_lib.erl', line: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< 0.70.0>,:monitor, {:nonode @ nohost,#PID< 0.81.0>},{:erlang,:apply, [#Function< 0.20003547 in file:test。 exs>,[" long string"]]}],[]}, {:主管,:do_start_child_i,3,[文件:' supervisor.erl',行: 381]},{:supervisor,:handle_call,3,[file:' supervisor.erl',line: 406]},{:gen_server,:try_handle_call,4,[file:' gen_server.erl', line:615]},{:gen_server,:handle_msg,5,[file:' gen_server.erl', line:647]},{:proc_lib,:init_p_do_apply,3,[file:' proc_lib.erl', line: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< 0.70.0>, {{:badmatch,{:error,{:EXIT,{:undef,[{Basic,:start_link, [#PID< 0.70.0>,:monitor,{:nonode @ nohost,#PID< 0.81.0>},{:erlang, :apply,[#Function< 0.20003547 in file:test.ex s>,[" long string"]]}], []},{:supervisor,:do_start_child_i,3,[file:' supervisor.erl', line:381]},{:supervisor,:handle_call,3,[file:' supervisor.erl', line:406]},{:gen_server,:try_handle_call,4,[f ile: ' gen_server.erl',line:615]},{:gen_server,:handle_msg,5,[文件: ' gen_server.erl',line:647]},{:proc_lib,:init_p_do_apply,3,[文件: ' proc_lib.erl',line: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', line:332]}]}}状态:{:state,{:local,MySupervisor}, :simple_one_for_one,[{:child,:undefined,Basic,{Basic,:start_link, []},:permanent,5000,:worker,[Basic]}],:undefined,3,5,[],0, MySupervisor,[]}
17:07:53.505 [错误] GenServer#PID< 0.79.0>终止 **(MatchError)右侧值不匹配:{:error,{:EXIT,{:undef,[{Basic,:start_link,[#PID< 0.70.0>,:monitor, {:nonode @ nohost,#PID< 0.81.0>},{:erlang,:apply, [#Function< 0.20003547 in file:test。 exs>,[" long string"]]}],[]}, {:主管,:do_start_child_i,3,[文件:' supervisor.erl',行: 381]},{:supervisor,:handle_call,3,[file:' supervisor.erl',line: 406]},{:gen_server,:try_handle_call,4,[file:' gen_server.erl', line:615]},{:gen_server,:handle_msg,5,[file:' gen_server.erl', line:647]},{:proc_lib,:init_p_do_apply,3,[file:' proc_lib.erl', line: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< 0.70.0>, {{:badmatch,{:error,{:EXIT,{:undef,[{Basic,:start_link, [#PID< 0.70.0>,:monitor,{:nonode @ nohost,#PID< 0.81.0>},{:erlang, :apply,[#Function< 0.20003547 in file:test.ex s>,[" long string"]]}], []},{:supervisor,:do_start_child_i,3,[file:' supervisor.erl', line:381]},{:supervisor,:handle_call,3,[file:' supervisor.erl', line:406]},{:gen_server,:try_handle_call,4,[f ile: ' gen_server.erl',line:615]},{:gen_server,:handle_msg,5,[文件: ' gen_server.erl',line:647]},{:proc_lib,:init_p_do_apply,3,[文件: ' proc_lib.erl',line: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', line:332]}]}}状态:{:state,{#PID< 0.79.0>,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]}]}}}
答案 0 :(得分:4)
Task.Supervisor
中的函数只能在以Task.Supervisor.start_link/1
开头的主管上运行。
您可以启动未命名的实例并将pid传递给async_stream_nolink/4
:
{:ok, supervisor} = Task.Supervisor.start_link
stream = Task.Supervisor.async_stream_nolink(supervisor, strings, fn text -> text |> String.codepoints |> Enum.count end)
或启动一个命名实例并传递其名称:
Task.Supervisor.start_link name: MySupervisor
stream = Task.Supervisor.async_stream_nolink(MySupervisor, strings, fn text -> text |> String.codepoints |> Enum.count end)
两种情况下的输出都是47
。
如果您希望此主管与您的应用程序一起运行,您可以将其添加到您的应用程序现有的顶级主管下,如下所示:
children = [
supervisor(Task.Supervisor, [name: MySupervisor]),
...
]
然后再做
Task.Supervisor.async_stream_nolink(MySupervisor, ...)