2016-09-08 58 views
-1

它接縫erlang只向遠程節點發送有趣的引用。當試圖發送閉包時,它顯然會在調用模塊中關閉閉包,並向遠程節點發送一個有趣的參考。這裏的測試:有沒有辦法在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 [email protected] with exit value: 
{undef,[{#Fun<tests.1.127565388>,[],[]}, 
     {funserver,loop,0,[{file,"funserver.erl"},{line,12}]}]} 
timeout 

所以可以Clojure中被髮送到遠程節點?

+0

在[erlang-questions mailing list]中的[this thread](http://erlang.org/pipermail/erlang-questions/2016-September/090046.html)中是否已經對此進行過深入討論? –

+0

[Erlang中的Spawn(Node,Fun)的重點是什麼,如果Node必須具有與客戶端節點相同的模塊可加載的內容,請執行以下操作):(http://stackoverflow.com/questions/39255471/what-is -the-點的spawnnode樂趣-ON-二郎-IF-節點有到有最同月齡) – mpm

回答

2

您的示例的問題在於客戶端節點編譯並將funserver模塊發送到遠程節點 - 但該模塊已存在並正在執行,正在等待接收消息 - 但它不會發送tests模塊,這是實際包含您發送的樂趣的模塊。

compile:file行中,將funserver更改爲tests,它應該工作。


此外,您還可以使用code:get_object_code,而不是compile:file,因爲模塊已經被編譯並在本地節點加載。

相關問題