Erlang rpc函數初學

剛開始學Erlang的函數調用,昨天晚上一直不知道rpc這個功能到底是什麼的,今天敲了一下,原來是這麼一回事:
-module(area_server1).
-export([loop/0,rpc/2]).
rpc(Pid,Request) ->
    Pid ! {self(),Request}, %%這是向指定Pid發送請求的
    receive  %%這個是接收響應結果的
        Response -> io:format("The answer is ~p~n",[Response])
    end.


loop() ->
    receive
        {From,{rec,Width,Ht}} -> 
            From ! Width*Ht,
            loop();

        {From,{circle,R}} -> 
            From ! 3.14*R,
            loop();

        {From,Other}->
            From ! {error,Other},
            loop()
    end.

~                

rpc的receive就是就是接受來自指定Pid發過來的信號的,原文中:就就是不做任何處理的把返回結果打印出來。
receive      
Response -> Response
end.


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章