Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Erlang distributed message sending - what is the meaning of the first atom in the tuple?

Tags:

erlang

The Erlang User's Guide describes the send operator as

8.9 Send

Expr1 ! Expr2

Sends the value of Expr2 as a message to the process specified by Expr1. The value of Expr2 is also the return value of the expression.

Expr1 must evaluate to a pid, a registered name (atom), or a tuple {Name,Node}. Name is an atom and Node is a node name, also an atom.

  • If Expr1 evaluates to a name, but this name is not registered, a badarg run-time error occurs.
  • Sending a message to a pid never fails, even if the pid identifies a non-existing process.
  • Distributed message sending, that is, if Expr1 evaluates to a tuple {Name,Node} (or a pid located at another node), also never fails.

In the case of distributed message sending, it is not clear to me what the first atom represents in {Name,Node} from the code of the remote node or process.

Your help is appreciated.

like image 380
lfurrea Avatar asked Oct 22 '12 23:10

lfurrea


People also ask

How does Erlang processes work?

In Erlang, each thread of execution is called a process. (Aside: the term "process" is usually used when the threads of execution share no data with each other and the term "thread" when they share data in some way. Threads of execution in Erlang share no data, that is why they are called processes).

What is a node in Erlang?

A node is an executing Erlang runtime system that has been given a name, using the command-line flag -name (long names) or -sname (short names).


2 Answers

The grammar is a bit ambiguous in the sentence you're citing. The three options are:

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

The first is by far the most common. Registered names are intended to be used judiciously.

I'd recommend starting with the concurrency chapter from Learn You Some Erlang, and backtracking as necessary to earlier chapters: http://learnyousomeerlang.com/the-hitchhikers-guide-to-concurrency#dont-panic

like image 155
macintux Avatar answered Sep 21 '22 13:09

macintux


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

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

{process1, node1@localhost} ! yourmessage.

Hope this will help

like image 34
user601836 Avatar answered Sep 24 '22 13:09

user601836