Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Erlang message loops

How does message loops in erlang work, are they sync when it comes to processing messages?

As far as I understand, the loop will start by "receive"ing a message and then perform something and hit another iteration of the loop.

So that has to be sync? right?

If multiple clients send messages to the same message loop, then all those messages are queued and performed one after another, or?

To process multiple messages in parallell, you would have to spawn multiple message loops in different processes, right?

Or did I misunderstand all of it?

like image 884
Roger Johansson Avatar asked May 31 '10 11:05

Roger Johansson


People also ask

Is Erlang multithreaded?

One of the main reasons for using Erlang instead of other functional languages is Erlang's ability to handle concurrency and distributed programming. By concurrency is meant programs that can handle several threads of execution at the same time.

How does Erlang processes work?

Erlang processes are lightweight, operate in (memory) isolation from other processes, and are scheduled by Erlang's Virtual Machine (VM). The creation time of process is very low, the memory footprint of a just spawned process is very small, and a single Erlang VM can have millions of processes running.

How does Erlang concurrency work?

Internally, Erlang handles concurrency by creating small, lightweight executions called 'processes. ' Unlike other major languages such as C, these processes are not built on top of the native operating system processes or the thread model, but rather created and managed internally by the Erlang virtual machine.

What is spawn in Erlang?

spawn() creates a new process and returns the pid. The new process starts executing in Module:Name(Arg1,...,ArgN) where the arguments are the elements of the (possible empty) Args argument list.


2 Answers

Sending a message is asynchronous. Processing a message is synchronous - one message is receive'd at a time - because each process has its own (and only one) mailbox.

like image 89
Frank Shearar Avatar answered Sep 23 '22 16:09

Frank Shearar


From the manual (Erlang concurrency

Each process has its own input queue for messages it receives. New messages received are put at the end of the queue. When a process executes a receive, the first message in the queue is matched against the first pattern in the receive, if this matches, the message is removed from the queue and the actions corresponding to the the pattern are executed.
However, if the first pattern does not match, the second pattern is tested, if this matches the message is removed from the queue and the actions corresponding to the second pattern are executed. If the second pattern does not match the third is tried and so on until there are no more pattern to test. If there are no more patterns to test, the first message is kept in the queue and we try the second message instead. If this matches any pattern, the appropriate actions are executed and the second message is removed from the queue (keeping the first message and any other messages in the queue). If the second message does not match we try the third message and so on until we reach the end of the queue. If we reach the end of the queue, the process blocks (stops execution) and waits until a new message is received and this procedure is repeated.
Of course the Erlang implementation is "clever" and minimizes the number of times each message is tested against the patterns in each receive.

So you could create prios with the regex, but the concurrency is done via multiple processes.

like image 43
weismat Avatar answered Sep 21 '22 16:09

weismat