Open vinhnglx opened 7 years ago
Processes are completely independent and isolated so how can they communicate? Processes will communicate via messages
.
So, in general, the progress will be, for example, we have two processes A and B
Kernel.send/2
to send the message. We also need to know process identifier PID before send.receive
construct.receive
construct works as follows:
after
clause is specified then run the code from after block.Example:
def receive_msg do
receive do
{:message, msg} -> IO.puts "Receive message from #{msg}"
end
end
iex(1)> pid = spawn(Practices, :receive_msg, [])
#PID<0.119.0>
iex(2)> send pid, {:message, "ding"}
Receive message from ding
{:message, "ding"}
Can refer #10 for the example
I. Process in Elixir
In BEAM, everything runs in a process.
BEAM process shouldn't be confused with an OS process.
Using
spawn/1
to create a new processAfter the process is created,
spawn
immediately returns. The result from this process will be returning soon.Two processes can't share any memory.
Remember, processes are completely independent and isolated.
Example: