fiv

fiv

How does message passing in Processes work?

Trying to wrap my head around how processes pass messages. I have a simple example of processA sending a message to processB

defmodule ProcessA do
  def start() do
    spawn fn -> run() end
  end

  def run do
    run()
  end

end
defmodule ProcessB do
  def start do
    spawn fn -> run() end
  end

  def run do
    run()
  end

end
iex(1)> process_a = ProcessA.start
#PID<0.153.0>
iex(2)> process_b = ProcessB.start
#PID<0.154.0>
iex(3)> send(process_b, "Hello")
"Hello"
iex(4)> Process.info(process_b, :messages)
{:messages, ["Hello"]}

When I use the send function the Iex process is sending a message to process_b.

I don’t understand how process_a can send a message to process_b.

First Post!

dimitarvp

dimitarvp

Every process can send another process a message if it knows how to reference it. Obviously that’s not doing being done willy-nilly but it’s a feature that is widely used and is a core tenet of the OTP.

Most Liked

sodapopcan

sodapopcan

I’m not sure but based on your naming scheme I think you may be conflating modules and processes. This is a fairly common mistake. A process is a distinct, self-contained concept that runs code and stores state whereas modules are merely static bags of functions that can be call from any process.

codeanpeace

codeanpeace

Yup!

You can send a message to process_a that tells it to send a message to process_b.

Here’s a quick example where the iex process sends a message to process_a which then sends that message to process_b along with the iex process pid so that process_b can notify the iex process that the message was received.

defmodule ProcessA do
  def start() do
    spawn fn -> run() end
  end

  def run do
    receive do
        {:send, recipient_pid, {origin_pid, message}} -> send(recipient_pid, {origin_pid, message})
    end
    run()
  end
end

defmodule ProcessB do
  def start do
    spawn fn -> run() end
  end

  def run do
    receive do
        {origin_pid, message} -> send(origin_pid, {self(), message})
    end
    run()
  end
end

iex(4)> iex_pid = self()
#PID<0.113.0>
iex(5)> process_a = ProcessA.start
#PID<0.134.0>
iex(6)> process_b = ProcessB.start
#PID<0.135.0>
iex(7)> send(process_a, {:send, process_b, {iex_pid, "hello world"}})
{:send, #PID<0.135.0>, {#PID<0.113.0>, "hello world"}}
iex(8)> flush
{#PID<0.135.0>, "hello world"}
:ok

Last Post!

Hermanverschooten

Hermanverschooten

Every process has a pid, if you know this pid you can send a message to it.
That is what you are doing when you write process_b = ProcessB.start.
You are saving the pid in the variable process_b.
There are several methods that allow you to make the pid of one process available to another, for example using the Registry.

Where Next?

Popular in Questions Top

ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New

Other popular topics Top

vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 40082 209
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
AngeloChecked
What learn first? Rust or Elixir Hi Elixir community! I’m here because i want learn a new language. I’m a junior developer and mainly i ...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New

We're in Beta

About us Mission Statement