Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ssh/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Process 如何从Erlang中派生的进程中获取返回值?_Process_Erlang_Distributed Computing_Spawn_Otp - Fatal编程技术网

Process 如何从Erlang中派生的进程中获取返回值?

Process 如何从Erlang中派生的进程中获取返回值?,process,erlang,distributed-computing,spawn,otp,Process,Erlang,Distributed Computing,Spawn,Otp,我有以下代码: -module(a). -compile(export_all). say(2,0) -> [1,2]; say(A,B) -> say(A-1,B-1). loop(0) -> io:format(""); loop(Times) -> L = spawn(a, say, [4,2]), io:fwrite( "L is ~w ~n", [L] ), loo

我有以下代码:

-module(a).
-compile(export_all).

say(2,0) ->
    [1,2];

say(A,B) ->
    say(A-1,B-1).

loop(0) ->
    io:format("");

loop(Times) ->
    L = spawn(a, say, [4,2]),
    io:fwrite( "L is ~w  ~n", [L] ),
    loop(Times-1).

run() ->
    loop(4).
每次函数“say”完成时,我都希望列表[1,2]在L中。但是,由于使用了spawn,因此返回的是进程的pid而不是函数的列表,因此我得到以下输出:

L is <0.113.0>  
L is <0.114.0>  
L is <0.115.0>  
L is <0.116.0>  

如何实现这一点?

您需要为此使用消息(或信号),因为代码在单独的进程中运行

在这种情况下,我喜欢使用spawn_monitor:

1> {Pid, MonitorReference} = spawn_monitor(fun() -> timer:sleep(10000), exit({ok, [1,2]}) end),
1> receive {'DOWN', MonitorReference, process, Pid, {ok, Result}} -> Result end.
请记住,您可以同时
接收多封邮件
,也可以只按顺序接收邮件(将不符合顺序的邮件留在邮箱中)。因此,您可以生成多个线程并等待所有线程完成,以收集结果:

工作(工作量)->
JobReference=make_ref(),
PidReferences=[spawn_monitor(fun()->exit({JobReference,do_stuff(WorkSlice)})end)| | WorkSlice Result;
{'DOWN',引用,进程,Pid,结果}->{error,Result}

end | |{Pid,Reference}要在进程之间传递信息,可以使用
将消息发送到另一个进程的邮箱,并使用
接收子句
从进程邮箱提取消息。以下是一个示例:

-module(a).
-compile(export_all).

%% Worker process:
say(From, 2, 0) ->
    From ! {self(), [1,2]};
say(From, A, B) ->
    say(From, A-1, B-1).


%%  Main process:
loop(0) ->
    ok;
loop(Times) ->
    Pid = spawn(a, say, [self(), 4, 2]),
    receive  %%waits here for result before spawning another process--no concurrency
        {Pid, Result} ->
            io:fwrite( "L is ~w  ~n", [Result] )
    end,
    loop(Times-1).


%%  Test:
run() ->
    loop(4).
在外壳中:

7> c(a).   
a.erl:2: Warning: export_all flag enabled - all functions will be exported
{ok,a}

8> a:run().
L is [1,2]  
L is [1,2]  
L is [1,2]  
L is [1,2]  
ok

9> 
或者,您可以生成所有进程,然后在它们进入时读取结果:

-module(a).
-compile(export_all).

%% Worker process:
say(From, 2, 0) ->
    From ! [1,2];
say(From, A, B) ->
    say(From, A-1, B-1).


%%  Main process:
loop(N) ->
    loop(N, N).

loop(0, Times) ->
    display_results(Times);
loop(N, Times) ->
    spawn(a, say, [self(), 4, 2]),
    loop(N-1, Times).
 
display_results(0) -> 
    ok;
display_results(Times) ->
    receive
        Result ->
            io:format("L is ~w~n", [Result])
    end,
    display_results(Times-1).

%%  Test:
run() ->
    loop(4).
要确保仅从生成的进程中接收
消息,可以执行以下操作:

-module(a).
-compile(export_all).

%% Worker process:
say(From, 2, 0) ->
    From ! {self(), [1,2]};
say(From, A, B) ->
    say(From, A-1, B-1).


%%  Main process:
loop(Times) ->
    loop(Times, _Pids=[]).

loop(0, Pids) ->
    display_results(Pids);
loop(Times, Pids) ->
    Pid = spawn(a, say, [self(), 4, 2]),
    loop(Times-1, [Pid|Pids]).


display_results([]) -> 
    ok;
display_results([Pid|Pids]) ->
    receive
        {Pid, Result} ->
            io:format("L is ~w~n", [Result])
    end,
    display_results(Pids).

%%  Test:
run() ->
    loop(4).
使用这样的
receive
时存在一些风险:如果工作进程在将消息发送到主进程之前崩溃,那么在等待崩溃进程的消息到达时,主进程将无限期地卡在接收中。一种解决方案:在接收中使用超时。另一种解决方案:使用spawn\u监视器()

-module(a).
-compile(export_all).

%% Worker process:
say(From, 2, 0) ->
    From ! {self(), [1,2]};
say(From, A, B) ->
    say(From, A-1, B-1).


%%  Main process:
loop(Times) ->
    loop(Times, _Pids=[]).

loop(0, Pids) ->
    display_results(Pids);
loop(Times, Pids) ->
    Pid = spawn(a, say, [self(), 4, 2]),
    loop(Times-1, [Pid|Pids]).


display_results([]) -> 
    ok;
display_results([Pid|Pids]) ->
    receive
        {Pid, Result} ->
            io:format("L is ~w~n", [Result])
    end,
    display_results(Pids).

%%  Test:
run() ->
    loop(4).