Nafets94

Nafets94

Elixir Multiple Processes

I’m having a hard time understanding how could I integrate processes in my application. I’ve done the Elixir Guide and I hope someone can help me understand this:

Can I have a process that spawns other processes for him to do his work? This “master” process sends two values a, b and an atom :add to some process, that process makes the sum and sends it back to the “master” process? I’m looking at all sorts of tutorials but can’t find any example in this matter.

Thank you.

Most Liked

kokolegorille

kokolegorille

The shell is already a process. The magic words are spawn, and send. You might also want to learn about Link and Monitor, which are the basic blocks, on which OTP is built upon.

$ iex
Erlang/OTP 21 [erts-10.2.3] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] [hipe]

Interactive Elixir (1.8.1) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)> self()
#PID<0.107.0>
iex(2)> calculate = fn a, b, _action -> IO.puts(a + b) end
#Function<18.128620087/3 in :erl_eval.expr/5>
iex(3)> spawn fn -> calculate.(1, 2, :add) end
3
#PID<0.111.0>

This example just print some output, but You can pass self() as a parameter to the message, and have the spawned process send back the result to the calling process.

iex(1)> pid = self()  
#PID<0.107.0>
iex(2)> calculate = fn a, b, pid -> send(pid, {:result, a + b}) end
#Function<18.128620087/3 in :erl_eval.expr/5>
iex(3)> spawn fn -> calculate.(1, 2, pid) end
#PID<0.111.0>
iex(4)> flush
{:result, 3}
:ok

You might like

https://www.cs.kent.ac.uk/ErlangMasterClasses/

in particular Master class 2, which is about turning sequential programming into concurrent.

sribe

sribe

Sure, you could do that. Now, whether or not there is any actual advantage to doing that depends on other factors: do you have 2 or more cores such that a & b can actually run simultaneously, or do a or b or both reach a point where they are waiting on I/O such that the other one can run in the gap?

Elixir processes make it very easy (and practical) to spawn multiple processes to take advantage of those kinds of situations, so easy that you can do so when there might be a chance but you’re not sure that such a situation occurs. Elixir (Erlang, really) make it easy to deal with a HUGE number of different processes in those types of situations,

Where Next?

Popular in Questions Top

greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
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
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lis...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
RisingFromAshes
I've read in another post that it may be possible with a router helper - but I couldn't find an appropriate one, and tbh, I'm still just ...
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
hariharasudhan94
Lets say i have map like this fetching from my database %{"_id" =&gt; #BSON.ObjectId&lt;58eb1a7a9ad169198c3dXXXX&gt;, "email" =&gt; "XX...
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
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
Nvim
Anybody knows a comprehensive comparison of Django and Phoenix, thanks for the help. Where are they similar? Where do they differ the m...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
Lily
In templates/appointment/index.html.eex: &lt;%= for appointment &lt;- @appointments do %&gt; &lt;tr&gt; &lt;td&gt;&lt;%= appoi...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum. ...
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
vegabook
I'm brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New
boundedvariable
I am going through the kafka architecture. All the features what the kafka is providing are already in Erlang. I would like hear your opi...
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New

We're in Beta

About us Mission Statement