Erlang distributed messaging

I have a question regarding sending distributed messages and a send statement. The erlang user guide describes the submit statement as

Expr1 ! Expr2 

And explains the case when Expr1 is a tuple of two atoms, the second is the name node, but it is not clear to me that the first atom represents the code of a remote node or process.

Your help is appreciated.

+4
source share
3 answers

In the sentence you are quoting, the grammar is a bit ambiguous. Three options:

  • The identifier of the process, which is an opaque data type returned from certain Erlang functions, primarily spawn and spawn_link.
  • The registered name on the local node (i.e. the local virtual machine). An example of where this will be needed is a long-term server application in which you want processes to be able to communicate with key service utilities, such as the DNS cache.
  • A tuple containing both the registered name and the node name, it lives (if another VM, possibly on a different host).

The first is by far the most common. Registered names are for reasonable use.

I would suggest starting with the concurrency chapter from Learn You Some Erlang and returning to the previous chapters: http://learnyousomeerlang.com/the-hitchhikers-guide-to-concurrency#dont-panic

+6
source

let's say you have two nodes: node1 @localhost and node2 @localhost, and you register the erlang process in node1 as process1.

You can send messages from node2 to process1 in node1 as:

 {process1, node1@localhost } ! yourmessage. 

Hope this helps

+3
source

In this expression:

Expr1! Expr2

Expr1 must evaluate (1) pid , (2) the registered name (atom), or (3) the tuple {Name, Node} .

In the third case, as you wanted to know, when Expr1 evaluates the tuple {Name, Node} , the name is the registered name (atom) of the process and Node is the name Node (also an atom), for example name @server.

For instance:

 % ------- in your node ------- ( you@server )> register(shell, self()). % ------- in my node ------- ( me@server )> {shell, you@server } ! "hey you!". 

Please note that before sending a message to other processes in other nodes, you must first connect to them. For example, using spawn (Node, Module, Function, Arguments) or net_adm: ping (Node) .

For testing purposes, use the nodes () function for a list of connected nodes.

+1
source

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


All Articles