Erlang/OTP Forums

Author Message

<  Erlang  ~  Remote Procedure Call

darkolee
Posted: Fri Feb 26, 2010 2:15 pm Reply with quote
User Joined: 02 Dec 2009 Posts: 12
Hi
I know that remote functions can be executed on a remote node using the rpc module or using the spawn() command itself.
As far as I know, the remote node however must have the Module implementing the function to be executed. Is it possible to execute a function on a remote node but only have the module with the function on the local (caller) node - and somehow this is implicitly sent to the remote node?

Thanks
View user's profile Send private message
zajda
Posted: Tue Mar 02, 2010 2:09 pm Reply with quote
User Joined: 22 Aug 2009 Posts: 83
well, there aren't any Higher Order Moduls, so sending Module is impossible.

However you can easily send function to be executed on remote node.

Take a look at this simple code:
Code:

%%% File    : entity.erl
%%% Author  : michal <michalzajda@gmail.com>
%%% Description : spawns and registers receive block
%%% Created :  2 Mar 2010 by michal <michalzajda@nixon>

-module(entity).

-export([start/0,something/1]).

start() ->
    Pid = spawn(?MODULE, something, [lists:seq(1,10)]),
    register(something, Pid).

something(Args) ->
    receive
        stop ->
            io:format("stop~n"),
            exit(normal);
        {exec, Fun} ->
            Result = Fun(Args),
            io:format("executed incoming fun: ~p~n", [Result]),
            something(Result);
        Message ->
            io:format("get a message ~p~n", [Message]),
            something(Args)
    end.





To play with this code open two erlang shells
(i.e.
Code:
erl -sname node1 -pa ebin
)

and try to send an anonymous fun from one to another:
Code:
Eshell V5.6.5  (abort with ^G)
(node1@nixon)1> entity:start().
true
(node1@nixon)2> net_kernel:connect(node0@nixon).
true
(node1@nixon)3> {something, node0@nixon} ! {exec, fun(List) -> [2*X||X<-List] end}.
{exec,#Fun<erl_eval.6.13229925>}


What you will see in the node0 shell is:

Code:
(node0@nixon)3> entity:start().
true
executed incoming fun: [2,4,6,8,10,12,14,16,18,20]


So, we initialised process with list of integers [1..10] and send a fun to double each element.

This is just simple example. Real system needs error and security handling.


Michal Zajda
------------------
Erlang Solutions Ltd.
View user's profile Send private message
col
Posted: Wed Jul 21, 2010 8:18 am Reply with quote
Joined: 21 Jul 2010 Posts: 3
Thanks for this interesting post but I have a problem sending and executing a function from node1 to node0 unless there is the same module compiled on both nodes.
For example the following code does not work (bad function exception):

(The following is running on node1)
Code:

-module(aserver).
-export([start/0]).
start() ->
   register(funcy,self()),
   io:format("Started! ~n", []),
   receive
      {Pid} ->
         io:format("Client Requested! ~n", []),
         io:format("connect! ~w ~n", [net_kernel:connect(node0@UniversityUser)]),
         Pid ! {exec,fun(B)->B+1 end}
   end.


(The following is running on node0)
Code:

-module(aclient).
-export([start2/0]).
start2() ->
   {funcy, node1@UniversityUser} ! {self()},
   receive
      {exec, F} ->
         io:format("Server responded! ~w  ~n", [F]),
         A = F(4),
         io:format("Answer: ~w  ~n", [A])
   end.


But if I make the module names identical the error changes to undefined function.
Finally if I have the same module name and copy the function start of the server in the client, the code works perfectly!

Any ideas please?
Thanks!
View user's profile Send private message
zajda
Posted: Wed Jul 21, 2010 9:41 am Reply with quote
User Joined: 22 Aug 2009 Posts: 83
Hi,

what I meant by 'send function' was kind of alternative approach to rpc. The fact is that in erlang we have only pointer to a fun. So, to execute something, it needs to be available in the context of target VM.

Now I see it is quite confusing, sorry for that.

So, the question is where to use this kind of approach. I think the best use-case is when we want to achieve bigger granularity in VM by spawning more processes, and ordering them what to do. It will help us utilize power of all cores.
View user's profile Send private message
fran
Posted: Wed Jul 21, 2010 10:10 am Reply with quote
Joined: 21 Jul 2010 Posts: 1
Does this mean that I cannot send functions as arguments across nodes without revealing its implementation (i.e. the receiver does not have a precompiled version of the function being sent)?

This is the setting that we have:

We have a client-server application (across nodes) whereby we want to construct the following "mobile code" protocol:
1) the client requests a service.
2) the server replies by sending a function.
3) the client runs the function (as a form of mobile code)

While doing this we want to keep the:
1) the client (module) as thin as possible.
2) abstract away as much as possible from the server's implmentation of the function being sent.

Can this be done? Does the semantics of function passing in Erlang differ depending on whether the communicating parties are on the same node or not?

Thanks,
View user's profile Send private message
zajda
Posted: Wed Jul 21, 2010 12:28 pm Reply with quote
User Joined: 22 Aug 2009 Posts: 83
take a look at doc:

http://www.erlang.org/faq/academic.html#id55627

Work around for your system, would be maintain all the functions you want to send in proper modules as a part of a separate application (lets say server or some kind of helper) which is only loaded on client machine. On server machine it might be started. To configure this, use rel file and pass next to application name atom load: {myapp, 1.0, load}.

The same problem exists in databases using map-reduce (i.e. Riak). The map phase cannot accept fun if it is send to remote nodes.
View user's profile Send private message
ryanmoritz
Posted: Wed Mar 28, 2012 2:15 pm Reply with quote
Joined: 28 Mar 2012 Posts: 4 Location: United States of America
That was amazing Surprised
View user's profile Send private message AIM Address
timrila
Posted: Thu Jun 07, 2012 10:01 am Reply with quote
User Joined: 28 Mar 2012 Posts: 32
Many thanks to the person who made this post, this was very informative for me. Please continue this awesome work.
Soccer Jerseys
Cheap Soccer Jerseys
Germany Soccer Jersey
Barcelona Jersey
View user's profile Send private message
wuji
Posted: Wed Sep 19, 2012 3:17 am Reply with quote
User Joined: 10 Aug 2012 Posts: 654
take effect for years, this method would not disable the the cheap replica *beep* the law quickly enough to appease the GOP and meet
Day One timeframe.Second, he could instruct his administration not to to [h1]cheap polo shirts[/h1] to enforce the law. This tactic is similar to Obama's
that his administration would no longer deport undocumented immigrants, "but "but cheap replica *beep* "but on steroids," Dorf said."It's not that he would be
one part of the act above another, he would be be authentic jordans be saying he wouldn't do any of it," Dorf said.Obama's
decision "was at the edge of what a president can can knockoff designer *beep* can do," he added. "What Romney would be proposing to
would go way beyond that."Romney's third and most "radical" option, option, real jordans option, Dorf said, would be to assert an independent duty
president to act on his own interpretation of the Constitution, Constitution, [h3]cheap replica *beep*[/h3] Constitution, despite the Supreme Court's ruling. Thomas Jefferson was the
View user's profile Send private message

Display posts from previous:  

All times are GMT
Page 1 of 1
This forum is locked: you cannot post, reply to, or edit topics.

Jump to:  

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum