【问题标题】:Example about Task.Supervisor.async_stream_nolink?关于 Task.Supervisor.async_stream_nolink 的示例?
【发布时间】: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


    【解决方案1】:

    Task.Supervisor 中的函数只能在以 Task.Supervisor.start_link/1 开头的 Supervisor 上运行。

    您可以启动一个未命名的实例并将 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, ...)
    

    【讨论】:

    • 操作!我编辑了你的答案而不是我的问题!
    • 我想你可能误读了我的回答——即使你传递了一个 pid,你也需要传递一个任务主管的 pid(Task.Supervisor.start_link,而不是 MySupervisor.start_link)。
    • 那么,我该如何定制呢?例如,失败的策略或把握过程..
    • Task.Supervisor.start_link/1 接受的一些选项记录在此:hexdocs.pm/elixir/Task.Supervisor.html#start_link/1
    • 嗨 Dogbert,你能在 elixir 论坛上查看这个问题吗:elixirforum.com/t/…
    猜你喜欢
    • 1970-01-01
    • 2016-01-17
    • 1970-01-01
    • 2015-11-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-16
    • 2019-10-26
    相关资源
    最近更新 更多