簡単な方法でメッセージを送受信できるように、それぞれが登録されている 3 つのプロセスがあるとします。
たとえば、10 回の反復を実行し、プロセス 1 と 3 の間でメッセージを前後に送信し、プロセス 2 を通過するループを作成するにはどうすればよいでしょうか? つまり、プロセス 1 からプロセス 2 にメッセージを送信し、プロセス 2 からプロセス 3 にメッセージを送信する必要があります。
簡単な方法でメッセージを送受信できるように、それぞれが登録されている 3 つのプロセスがあるとします。
たとえば、10 回の反復を実行し、プロセス 1 と 3 の間でメッセージを前後に送信し、プロセス 2 を通過するループを作成するにはどうすればよいでしょうか? つまり、プロセス 1 からプロセス 2 にメッセージを送信し、プロセス 2 からプロセス 3 にメッセージを送信する必要があります。
これは、探していることを行うための小さなモジュールです。しかし、あなたが多くの答えを探しているように見えたので、おそらくあなたは erlang に慣れていないと思います。ですから、学習曲線を始めるには、本やオンラインの本を見るべきだと思います。私のお気に入りは次のとおりです: 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.
そしてシェルで:
(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>
あなたは、あなたがそれをN
何回も実行する関数を書くことができます:
times(0, Fun) -> ok;
times(N, Fun) -> Fun(), times(N - 1, Fun).
次に、プロセス 1 のコードで呼び出します。
times(10, fun() -> process2 ! SomeMessage end)
そして、プロセス 2 のコードは、このメッセージを処理するためにそれを知る必要があります。たとえば、プロセス 3 に何かを送信します。