有没有一种方法可以向erlang中的远程节点发送闭包?
它只向远程节点发送有趣的引用。当试图发送闭包时,它显然将闭包内联到调用模块中,并将一个fun ref发送到远程节点。以下是测试:有没有一种方法可以向erlang中的远程节点发送闭包?,erlang,distributed,Erlang,Distributed,它只向远程节点发送有趣的引用。当试图发送闭包时,它显然将闭包内联到调用模块中,并将一个fun ref发送到远程节点。以下是测试: -module(funserver). -compile(export_all). loop()-> receive {From, ping} -> error_logger:info_msg("received ping from ~p~n", [From]), From ! pong, loop();
-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
节点上的进程中出现错误servas@sharas具有退出值:
{undef,[{#Fun,[],[]},
{funserver,loop,0,[{file,“funserver.erl”},{line,12}]}
超时
那么clojure可以被发送到远程节点吗?您的示例中的问题是,客户端节点编译并将
funserver
模块发送到远程节点-但该模块已经在那里并正在执行,等待接收消息-但它不发送tests
模块,这是一个模块,它实际上包含了你正在传递的乐趣
在compile:file
行中,将funserver
更改为tests
,它应该可以工作
此外,您可以使用
code:get\u object\u code
而不是compile:file
,因为模块已经编译并加载到本地节点中。erlang questions邮件列表中不是已经详细讨论了这一点吗?可能重复