[英]Can not spawn function on remote node with spawn(Node, Fun) in erlang
我尝试使用分布式erlang,这是我所拥有的:
loop()->
receive {From, ping} ->
io:format("received ping from ~p~n", [From]),
From ! pong,
loop();
{From, Fun} when is_function(Fun) ->
io:format("executing function ~p received from ~p~n", [Fun, From]),
From ! Fun(),
loop()
end.
test_remote_node_can_execute_sent_clojure()->
Pid = spawn(trecias, fun([])-> loop() end),
Pid ! {self(), fun()-> erlang:nodes() end},
receive Result ->
Result = [node()]
after 300 ->
timeout
end.
得到: Can not start erlang:apply,[#Fun<tests.1.123107452>,[]] on trecias
我在与节点“ trecias”相同的机器上运行的节点上执行测试。 两个节点都可以加载相同的代码。
有什么主意吗?
在spawn
调用中,您已将节点名称指定为trecias
,但是您需要指定完整的节点名称,包括主机名,例如trecias@localhost
。
同样,传递给spawn/2
的函数必须使用零个参数,但是上面代码中的一个接受一个参数(如果该参数不是空列表,则会崩溃)。 写成fun() -> loop() end
。
在远程节点上生成匿名函数时,还需要确保模块在两个节点上均以相同的版本加载。 否则,您将收到badfun
错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.