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

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
LegitStack
I’m hoping you guys can give me some general advice and perhaps code examples if you’re feeling up to it. I’m very interested in Elixir,...
New
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
_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list....
New
Fl4m3Ph03n1x
Background Let’s assume I have a typical GenServer that receives messages as requests, does some operation in a DB and returns responses....
New
gonzofish
I’m currently trying to understand how to join three tables using Ecto. All the examples I’ve seen use 2, so maybe I’m just missing somet...
New
johnnyicon
Hi all, I've just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I'm trying to use Postg...
New
sabri
Can someone explain the settings of pool_size of Ecto in config file? and what is the recommend size? Thanks
New

Other popular topics Top

sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
977 41022 311
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 35421 110
New
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod -- where is this set? Thanks.
New
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
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
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
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New

We're in Beta

About us Mission Statement