hunar

hunar

Broadcasting a message to multiple linked processes without pid?

Hi - sorry for another beginner question…

Is it possible in Elixir to spawn e.g process1, process2, process3, and process4
Then link process1 with both process2 and process4 but not process3
Then process1 broadcast a message e.g {:greeting “hello”} without knowing pids of the linked processes, and that message is received only by the linked processes 2 & 4?

A code snippet or a link of an example demonstrate the above is deeply appreciated.

Thanks in advance
Hunar

Most Liked

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

Can you speak a bit more about what you’re trying to do at a higher level? Links are used to establish lifecycle constraints, not really to setup messaging routes or something. If you can articulate what you’re after we can suggest a more idiomatic approach.

peerreynders

peerreynders

https://github.com/CorticalComputer/Book_NeuroevolutionThroughErlang/blob/master/Ch_6/simplest_nn.erl

shows the simplest approach that was used for the Handbook of Neuroevolution Through Erlang. Each process holds the pids of the other processes it needs to communicate within its state.

  • The sensor process sends messages to its neuron process (N_PId).
  • The neuron process expects messages from its sensor process (S_PId) and sends messages to its actuator (A_PId) process.
  • The actuator process receives messages from its neuron process (N_PId).
  • The cortex process sends a sync message to the sensor process (Sensor_PId) to initiate the sense_think_act cycle. It knows all three (Sensor_PId,Neuron_PId,Actuator_PId) so it can terminate them.
Qqwy

Qqwy

TypeCheck Core Team

In this case, you could maybe use pg2, which is an Erlang built-in module that handles process groups, allowing processes to register under a given name, and then sending a message to one, all or some group members.

Another thing you might try, is to use something like Flow, which allows you to work with a producer-consumer-type interface directly. So after setting up the connections and specifying what each of the neurons should do, Flow will handle the inter-process passing on of messages for you.

But maybe the neurons of a neural network are not the proper level of abstraction to split it into multiple processes, since each of these neurons is probably extremely simple: If you have a system in which most of the processes cannot do a lot of work without communicating with a large number of other processes, the message-passing part itself might become more of a bottleneck than what you solve by splitting your neural network up into processes like this.

OvermindDL1

OvermindDL1

Another option, especially if it is a multi-directional graph, would be to use the built-in :graph module to hold the PID ‘links’ then with a wrapper module around that then you could ‘broadcast’ to all that are linked to you or vice-versa. If the graph is built once and never/rarely changed that embedding it into that module would be super fast, otherwise could probably encode it into ETS carefully as well sans the graph module).

If however it is just a simple list of send to things in this group, then pg2 or gproc (or maybe Registry) would be dead simple (I’d probably use gproc, but only because I know it best).

Qqwy

Qqwy

TypeCheck Core Team

As @benwilson512 noted, the ‘linking’ process that is normally used is only to ensure that two processes that depend on each other will die as a whole when one of them dies.

The linking you are referring to would just mean exchanging the PIDs and sending a message to the PIDs you know at a later time. Why would it be wrong here to ‘bother about PIDs’? You can see the neural network represented then in the same way as a social network is represented with people using phones: When I get an important message, I can (and will) send it to the people whose phone number I have. PIDs are processes’ phone numbers, nothing more, nothing less.

Where Next?

Popular in Questions Top

joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 record...
New
Werner
Hi, I’m using Ubuntu 18.04 and after updating to OTP-24.0 yesterday i have this warning when I run “mix local.hex”: 14:57:30.512 [warn] ...
New
quazar
How to set Jason to encode all fields in ecto schema, I don’t care about security and implementing only is taking long list of attributes...
New
mgjohns61585
Could someone help me? I'm making my first elixir program, number guessing game. I can't figure out how to convert the user's guess from ...
New
makeitrein
Hey all, just started picking up Elixir last week and am writing a scraper as a learning project. Baby step #1 is extracting the number ...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
kostonstyle
Hi all I want to have a unix time, from the current time plus 1 hour. DateTime.now + 1 hour How to get it in elixir? Thanks
New
Exadra37
Sometimes I want to check if the input into a function is not a blank string. My first approach: defmodule Example do def do_stuff(s...
New

Other popular topics Top

srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call th...
New
dotdotdotPaul
Okay, I'm having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I'm sure I'...
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
yawaramin
In the Dialyzer docs ( http://erlang.org/doc/man/dialyzer.html#requesting-or-suppressing-warnings-in-source-files ), there is a way to tu...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
vac
Hi, I'm quite new in Elixir and I'm trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and ...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
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
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New

We're in Beta

About us Mission Statement