In the last days I have been learning about Process in Elixir. After doing some exercises and understand some things about it, I can explain how to create a simple example about process.
The Process
Work with process in Elixir is so common, so, it’s important know how to use it. For create a process you can use spawn
(which takes a function).
The process structure is very simple: you have a mailbox, created within a function and by the word
receive
, and you can manage different messages and do something. When you create a process withspawn
, you will get a Process ID, and also you can know if this process is alive.
When you have a process alive, you can send it messages, for do this you only have to send with the process ID and a message (a data structure, a tuple, an atom…) .
For make this more clearly I will show how to create an example.
Ping Pong Example With a Process
We have to create a simple function and create a receiver
, within this, we are going to use the patter matching for receive different types of messages, in this case we are to receive two atoms: :ping
and :pong
. When we receive this, we will print a message.
If we run the iex
shell we can create a process with this function and send it a messages for print the ping
and pong
:
You will see that only in the first message sent we can see the printed message
Process Alive
This happens because the process created was killed after receive the first message. We can know if the process is alive in the iex
shell:
The solution for avoid this is call the same function in the receiver, with this the process won’t be killed after receive a message.
We are sending a message to a process alive. Now I want that this process send messages to the same process for do the ping-pong
.
Ping Pong in the same Process
My first try is that when the process receive a message, send the other message to the same process.
For this we only have to include the process ID in the tuple, and make a send with the other message.
I’m going to print the process id in the function using self()
.
Ping Pong with Two Process
Now we have to create the ping-pong
example with two process.
For do this we can create two process of the same function. Instead of get a single process id in the receiver
we can receive two process Id.
We can see which process are print the ping
or pong
message.