有没有办法在erlang中向远程节点发送闭包?

时间:2016-09-08 16:55:44

标签: erlang distributed

它接缝erlang只发送对远程节点的有趣引用。当尝试发送闭包时,它显然在调用模块中内联闭包并向远程节点发送有趣的参考内联乐趣。这是测试:

-module(funserver).

-compile(export_all).

loop()->
receive {From, ping} ->
        error_logger:info_msg("received ping from ~p~n", [From]),
        From ! pong,
        loop();
    {From, Fun} when is_function(Fun) ->
        error_logger:info_msg("executing function ~p received from ~p~n", [Fun, From]),
        From ! Fun(),
        loop();
    M -> 
        error_logger:error_msg("received ~p, don't know what to do with it", [M])
end.

和客户端节点上的测试:

test_remote_node_can_execute_sent_clojure()->
    {ok, ModName, Binary} = compile:file(funserver, [verbose,report_errors,report_warnings, binary]),
    {module, ModName} = rpc:call(?other_node, code, load_binary, [ModName, atom_to_list(ModName), Binary]),
    Pid = spawn(?other_node, funserver, loop, []),
    OutVar = {"token with love from", node()},
    Pid ! {self(), fun()-> {OutVar, erlang:node()} end},
    receive Result -> 
        Result = {OutVar, node(Pid)}
    after 300 ->
              timeout
    end.

获得

Error in process <7162.123.0> on node servas@sharas with exit value:
{undef,[{#Fun<tests.1.127565388>,[],[]},
        {funserver,loop,0,[{file,"funserver.erl"},{line,12}]}]}
timeout

那么可以将clojure发送到远程节点吗?

1 个答案:

答案 0 :(得分:2)

您的示例的问题是客户端节点编译并将funserver模块发送到远程节点 - 但该模块已经存在并正在执行,等待接收消息 - 但它不会发送tests模块,它是实际包含您发送的乐趣的模块。

compile:file行中,将funserver更改为tests,它应该有效。

此外,您可以使用code:get_object_code而不是compile:file,因为模块已经在本地节点中编译和加载。