如果Node必须具有与客户机节点相同的模块可加载,那么在erlang上的“Spawn(节点,乐趣”)是什么意思? [英] What is the point of Spawn(Node, Fun) on erlang, if Node has to have the same module loadable as a client node?
问题描述
让我们考虑两种可能的情况
引用模块功能的函数
Fun = fun file:getcwd / 0,
erlang:spawn(Node,Fun)。
在这种情况下, Fun
确实应该是可加载的在遥远的一边。
匿名函数
Fun = fun() - > io:format(我的节点是〜p〜n,[node()])end,
erlang:spawn(Node,Fun)。
他们也是可调用。
总结
虽然对功能有一些限制,可以远程处理,但此形式仍然适用,可以删除,因为第二个案例将变得不可能
另外
有些误解可能来自于这个粒子
其实,如果你跑 erlang:fun_info
用于匿名函数,您会看到它以AST的形式提供实现
(b @ lol4t0到户)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,612,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& ,
{value,#Fun< shell.5.31625193>},
[{clause,1,[],[],
[{call,1,
{remote,1,{atom,1,io},{atom,1,format}},
[{string,1,我的节点是〜p〜n},
{cons,1,{call,1,{atom,1,node},[]},{nil,1}}}}}}}},
{type,local}]
Why create illusion that you are sending a Fun to remote node to execute in a new process? If client node has to have same module loadable with the Fun defined as a server node anyway. Why not only spawn(Node, M, F, A) then, which makes it clear that you are sending a definition of a function call, not Fun itself.
Let's consider two possible cases
Functions referring to module functions
Fun = fun file:getcwd/0,
erlang:spawn(Node, Fun).
In this case Fun
indeed should be loadable at the remote side.
Anonymous functions
Fun = fun() -> io:format("My node is ~p~n", [node()]) end,
erlang:spawn(Node, Fun).
They are callable also.
Summarizing
While there some limitations implied to a function, that can be spwaned remotely, this form is still suitable and can be dropped, because then second case would become impossible
Also
Some misconceptions might go from this article
Actually, if you run erlang:fun_info
for anonymous function, you'll see, that it provides implementation in a form of 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}]
这篇关于如果Node必须具有与客户机节点相同的模块可加载,那么在erlang上的“Spawn(节点,乐趣”)是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!