最好使用模块rpc,因为如果你不这样做:你必须管理远程节点的监控,必须提供唯一的调用ID,处理超时,你也有为带有函数结果的回送响应提供包装器。
但是所有这些操作都是通用的,并在rpc 模块中实现。
顺便说一下,远程调用有不同的变体,在rpc中实现:同步和异步调用,cast(发送不需要响应的消息),甚至并行地图功能(pmap)。
附言
比较 - 简单地使用 rpc:call 与从头开始实现(另外,这是简单的实现,它不处理一些重要的情况):
-module(myrpc).
-compile(export_all).
server() ->
receive
{{CallerPid, Ref}, {Module, Func, Args}} ->
Result = apply(Module, Func, Args),
CallerPid ! {Ref, Result}
end.
call(Node, Module, Func, Args) ->
monitor_node(Node, true),
RemotePid = spawn(Node, ?MODULE, server, []),
Ref = make_ref(),
RemotePid ! {{self(), Ref}, {Module, Func, Args}},
receive
{Ref, Result} ->
monitor_node(Node, false),
Result;
{nodedown, Node} ->
error
end.