如果Node必须具有与客户端节点相同的模块,那么在erlang上Spawn(Node,Fun)有什么意义呢?

Sha*_*ras 4 erlang distributed

为什么要创建一个幻觉,您正在向远程节点发送Fun以在新进程中执行?如果客户端节点必须具有相同的模块可加载,则无论如何都将Fun定义为服务器节点.那么为什么不只生成(Node,M,F,A),这清楚地表明你正在发送一个函数调用的定义,而不是Fun本身.

Lol*_*4t0 5

让我们考虑两种可能的情况

涉及模块功能的功能

Fun = fun file:getcwd/0,
erlang:spawn(Node, Fun). 
Run Code Online (Sandbox Code Playgroud)

在这种情况下,Fun确实应该可以在远程端加载.

匿名功能

Fun = fun() -> io:format("My node is ~p~n", [node()]) end,
erlang:spawn(Node, Fun). 
Run Code Online (Sandbox Code Playgroud)

它们也可以调用.

总结

虽然某个功能存在一些限制,可以远程处理,但这种形式仍然适用并且可以删除,因为第二种情况将变得不可能

本文可能会有一些误解

实际上,如果你运行erlang:fun_info匿名函数,你会看到它以AST的形式提供实现

(b@lol4t0-home)21> rp(erlang:fun_info(fun() -> io:format("My node is ~p~n", [node()]) end)). 
[{pid,<0.96.0>},
 {module,erl_eval},
 {new_index,20},
 {new_uniq,<<99,62,121,82,122,95,246,237,63,72,118,40,4,
             25,16,50>>},
 {index,20},
 {uniq,52032458},
 {name,'-expr/5-fun-3-'},
 {arity,0},
 {env,[{[],
        {eval,#Fun<shell.21.31625193>},
        {value,#Fun<shell.5.31625193>},
        [{clause,1,[],[],
                 [{call,1,
                        {remote,1,{atom,1,io},{atom,1,format}},
                        [{string,1,"My node is ~p~n"},
                         {cons,1,{call,1,{atom,1,node},[]},{nil,1}}]}]}]}]},
 {type,local}]
Run Code Online (Sandbox Code Playgroud)