Erlang: remote calling and sending messages

I would like to execute some procedure on a remote node. And I'm not sure if this is the best way to do this. I can write rpc:call for this. Or send a message to Remote ! {call, some_procedure} Remote ! {call, some_procedure} in node to start the procedure, and use receive while waiting for a response. So which way is better in erlang? Or are they really designed for different uses?

+4
source share
2 answers

It is better to use the rpc module, because if you do not: you will need to manage the monitoring of the remote node, provide a unique call identifier, processing timeouts, and you will also have to provide a wrapper for feedback feedback from the function result.

But all these manipulations are common and implemented in the rpc module.

By the way, there are various options for remote calls that are implemented in rpc : synchronous and asynchronous calls , cast (send a message that does not require an answer), even the parallel card function ( pmap ).

PS

Compare - just using rpc: call against an implementation from scratch (also this is a simple implementation that does not handle some important cases):

 -module(myrpc). -compile(export_all). server() -> receive {{CallerPid, Ref}, {Module, Func, Args}} -> Result = apply(Module, Func, Args), CallerPid ! {Ref, Result} end. call(Node, Module, Func, Args) -> monitor_node(Node, true), RemotePid = spawn(Node, ?MODULE, server, []), Ref = make_ref(), RemotePid ! {{self(), Ref}, {Module, Func, Args}}, receive {Ref, Result} -> monitor_node(Node, false), Result; {nodedown, Node} -> error end. 
+9
source

rpc seems to be a complete solution, but has some drawbacks associated with the scale. rpc uses a single "rex" server for cross node communication and can potentially be overloaded. If you are using rpc, you should follow this process.

If communication is the core functionality, and it is the pinnacle of io / cpu / memory consumer, I would consider writing it. On the other hand, we can expect improvement from the OTP team (and preliminary mature optimization is the root of all evil !!!).

+5
source

Source: https://habr.com/ru/post/1435223/


All Articles