在Erlang中具有预定义迭代次数的消息循环

时间:2012-12-07 07:41:24

标签: erlang distributed distributed-computing

假设我有3个进程,每个进程都已注册,以便我可以轻松地发送和接收消息。

如何创建一个循环,例如10次迭代,并在进程1和进程3之间向前和向后发送消息,进行进程2?这意味着,我希望进程1向进程2发送一条消息,进程2应该将消息发送到进程3,反之亦然。

2 个答案:

答案 0 :(得分:4)

这是一个小模块,可以满足您的需求。但是当我看到你正在寻找许多答案时,我猜你是二郎的新手。所以我认为你应该看书或在线书籍来开始你的学习曲线。我首选的是: http://learnyousomeerlang.com/content 作者做得很好,帮助初学者开始使用Erlang。当你对erlang越来越熟悉的时候,官方文档也很有意思(尽管开始没用!)。

-module(ping3).
-compile(export_all).

% Launch 3 loops, init phase
start() ->
    P1 = spawn(?MODULE,loop,[]),
    P2 = spawn(?MODULE,loop,[]),
    P3 = spawn(?MODULE,loop,[]),
    {P1,P2,P3}.

% user interface
start_ring(P1,P2,P3,Loop) ->
    P1 ! {start,P2,P3,Loop}.

% kind of server, the messages content the informations about what to do, and all needed parameters
% it implements your protocol
loop() ->
    receive
        {start,By,To,Loop} ->
            Ref = make_ref(),
            io:format("start ring for ~p iterations of ~p~n",[Loop,Ref]),
            By ! {go_via,Ref,self(),By,To,Loop},
            loop();
        {go_via,Ref,From,By,To,Loop} ->
            To ! {go,Ref,From,By,To,Loop},
            loop();
        {go,Ref,From,By,To,Loop} ->
            By ! {back_via,Ref,From,By,To,Loop},
            loop();
        {back_via,Ref,From,By,To,Loop} ->
            To ! {back,Ref,From,By,To,Loop},
            loop();
        {back,Ref,_,_,_,0} ->
            io:format("end of ring of ~p~n",[Ref]),
            loop();
        {back,Ref,From,By,To,Loop} ->
            io:format("continue ring for ~p iterations of ~p~n",[NewLoop=Loop-1,Ref]),
            By ! {go_via,Ref,From,By,To,NewLoop},
            loop();
        stop -> bye
    end.

并在shell中:

(exec@WXFRB1824L)48> {P1,P2,P3} = ping3:start().
{<0.93.0>,<0.94.0>,<0.95.0>}
(exec@WXFRB1824L)49> ping3:start_ring(P1,P2,P3,20),ping3:start_ring(P2,P1,P3,15).     
start ring for 20 iterations of #Ref<0.0.1.91077>
start ring for 15 iterations of #Ref<0.0.1.91080>
{start,<0.93.0>,<0.95.0>,15}
continue ring for 19 iterations of #Ref<0.0.1.91077>
continue ring for 14 iterations of #Ref<0.0.1.91080>
continue ring for 18 iterations of #Ref<0.0.1.91077>
continue ring for 13 iterations of #Ref<0.0.1.91080>
continue ring for 17 iterations of #Ref<0.0.1.91077>
continue ring for 12 iterations of #Ref<0.0.1.91080>
continue ring for 16 iterations of #Ref<0.0.1.91077>
continue ring for 11 iterations of #Ref<0.0.1.91080>
continue ring for 15 iterations of #Ref<0.0.1.91077>
continue ring for 10 iterations of #Ref<0.0.1.91080>
continue ring for 14 iterations of #Ref<0.0.1.91077>
continue ring for 9 iterations of #Ref<0.0.1.91080>
continue ring for 13 iterations of #Ref<0.0.1.91077>
continue ring for 8 iterations of #Ref<0.0.1.91080>
continue ring for 7 iterations of #Ref<0.0.1.91080>
continue ring for 6 iterations of #Ref<0.0.1.91080>
continue ring for 5 iterations of #Ref<0.0.1.91080>
continue ring for 4 iterations of #Ref<0.0.1.91080>
continue ring for 3 iterations of #Ref<0.0.1.91080>
continue ring for 2 iterations of #Ref<0.0.1.91080>
continue ring for 1 iterations of #Ref<0.0.1.91080>
continue ring for 0 iterations of #Ref<0.0.1.91080>
end of ring of #Ref<0.0.1.91080>
continue ring for 12 iterations of #Ref<0.0.1.91077>
continue ring for 11 iterations of #Ref<0.0.1.91077>
continue ring for 10 iterations of #Ref<0.0.1.91077>
continue ring for 9 iterations of #Ref<0.0.1.91077>
continue ring for 8 iterations of #Ref<0.0.1.91077>
continue ring for 7 iterations of #Ref<0.0.1.91077>
continue ring for 6 iterations of #Ref<0.0.1.91077>
continue ring for 5 iterations of #Ref<0.0.1.91077>
continue ring for 4 iterations of #Ref<0.0.1.91077>
continue ring for 3 iterations of #Ref<0.0.1.91077>
continue ring for 2 iterations of #Ref<0.0.1.91077>
continue ring for 1 iterations of #Ref<0.0.1.91077>
continue ring for 0 iterations of #Ref<0.0.1.91077>
end of ring of #Ref<0.0.1.91077>
(exec@WXFRB1824L)50> 

答案 1 :(得分:1)

你可以编写一个函数来执行你告诉它N次的任何事情:

times(0, Fun) -> ok;
times(N, Fun) -> Fun(), times(N - 1, Fun).

然后在进程1的代码中调用它:

times(10, fun() -> process2 ! SomeMessage end)

进程2的代码需要知道处理此消息,例如发送一些东西到处理3。