尝试应用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
Run Code Online (Sandbox Code Playgroud)
但是,我得到了错误:
**(从 #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 string"]]}]}, :infinity) ** (EXIT) 没有进程:进程不是活动或当前没有与给定名称关联的进程,可能是因为其应用程序未启动
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>, ["长字符串"]]}]}, :infin ity) ** ( EXIT) 无进程:进程不存在或当前没有与给定名称关联的进程,可能是因为其应用程序未启动 最后一条消息:{: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在文件:test.exs
, ["长字符串"]]}]}, :infinity]}}} 状态: {:state, {#PID<0.79.0>, MySupervisor}, :one_for_one, [{:child, #PID<0.80.0 >, 基本, {基本, :start_link, []}, :永久, 5000, :worker, [基本]}], :未定义, 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
Run Code Online (Sandbox Code Playgroud)
但我得到了错误:
**(从 #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:test.exs>, ["长字符串"]]}], []}, {:supervisor, :do_start_child_i, 3, [文件: '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]}]}}}(elixir)lib/task/supervisor.ex:265:匿名fn /4 在 Task.Supervisor.build_stream/5 (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>, ["长字符串"]]}], []}, {:supervisor, :do_start_child_i, 3, [文件: '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]}]}}} (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>, ["长字符串"]]}], []}, {:supervisor, :do_start_child_i, 3, [文件: '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]}]}}} (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>, ["长字符串"]]}], []}, {:supervisor, :do_start_child_i, 3, [文件: '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,[文件:'lib/task/supervisor.ex',行:265 ]}, {Task.Supervised, :stream_monitor, 6, [文件: 'lib/task/supervised.ex', 行: 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>, ["长字符串"]]}], []}, {:supervisor, :do_start_child_i, 3, [文件: '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]}]}}} (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>, ["长字符串"]]}], []}, {:supervisor, :do_start_child_i, 3, [文件: '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,[文件:'lib/task/supervisor.ex',行:265 ]}, {Task.Supervised, :stream_monitor, 6, [文件: 'lib/task/supervised.ex', 行: 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.Supervisor只能在以 启动的 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)
Run Code Online (Sandbox Code Playgroud)
或启动一个命名实例并传递其名称:
Task.Supervisor.start_link name: MySupervisor
stream = Task.Supervisor.async_stream_nolink(MySupervisor, strings, fn text -> text |> String.codepoints |> Enum.count end)
Run Code Online (Sandbox Code Playgroud)
两种情况的输出都是47。
如果您希望此主管与您的应用程序一起运行,您可以将其添加到应用程序现有的顶级主管下,如下所示:
children = [
supervisor(Task.Supervisor, [name: MySupervisor]),
...
]
Run Code Online (Sandbox Code Playgroud)
然后做
Task.Supervisor.async_stream_nolink(MySupervisor, ...)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1758 次 |
| 最近记录: |