如果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?

查看:115
本文介绍了如果Node必须具有与客户机节点相同的模块可加载,那么在erlang上的“Spawn(节点,乐趣”)是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么要创建一个幻想,你发送一个乐趣到远程节点执行一个新的过程?如果客户端节点必须具有与定义为服务器节点的Fun相同的模块可加载。为什么不仅生成(Node,M,F,A),这使得您清楚地发送一个函数调用的定义,而不是Fun本身。

解决方案

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



引用模块功能的函数



  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屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆