简体   繁体   English

无法使用erlang中的spawn(Node,Fun)在远程节点上生成功能

[英]Can not spawn function on remote node with spawn(Node, Fun) in erlang

experimenting with distributed erlang, here's what I have: 我尝试使用分布式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.

getting: Can not start erlang:apply,[#Fun<tests.1.123107452>,[]] on trecias 得到: Can not start erlang:apply,[#Fun<tests.1.123107452>,[]] on trecias

node I execute the test on runs on the same machine as the node 'trecias'. 我在与节点“ trecias”相同的机器上运行的节点上执行测试。 Both nodes can load same code. 两个节点都可以加载相同的代码。

Any ideas what is amiss? 有什么主意吗?

In the spawn call, you've specified the node name as trecias , but you need to specify the full node name including the hostname, eg trecias@localhost . spawn调用中,您已将节点名称指定为trecias ,但是您需要指定完整的节点名称,包括主机名,例如trecias@localhost

Also, the function you pass to spawn/2 must take zero arguments, but the one in the code above takes one argument (and crashes if that argument isn't the empty list). 同样,传递给spawn/2的函数必须使用零个参数,但是上面代码中的一个接受一个参数(如果该参数不是空列表,则会崩溃)。 Write it as fun() -> loop() end instead. 写成fun() -> loop() end

When spawning an anonymous function on a remote node, you also need to make sure that the module is loaded on both nodes, with the same version. 在远程节点上生成匿名函数时,还需要确保模块在两个节点上均以相同的版本加载。 Otherwise you'll get a badfun error. 否则,您将收到badfun错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM