mattfara50

mattfara50

Question about spawn/1

I’m reading through Elixir in Action. In the chapter of concurrency primitives, it mentions that when calling send/2, the second argument, as a term, gets shoved into the receiving processes’s mailbox. So here’s the Q: when you run spawn/1, a new process is created, and the argument is a 0-arity lambda. Is the lambda term also placed into the new processes’s mailbox, like a startup message?

Secondarily, how important is understanding these low-level details for the day-to-day programming of Elixir? I’m just starting up, so I’d rather be useful before I’m knowledgeable, if that’s possible.

Marked As Solved

caleb-bb

caleb-bb

No, the lambda does not go into the new process’ mailbox. It is, rather, executed right away. The purpose of the mailbox is communication between processes. The purpose of the lambda is to basically define what the process created by spawn/1 actually does. So that lambda is a pure function. The way you make it dynamic is by writing a function that dynamically defines the lambda when it runs and passes that lambda to spawn/1. In Elixir In Action, they define async_query/1 immediately after they introduce spawn/1 (pg. 137 of my copy). The async_query/1 takes an argument that winds up being part of the lambda definition, which is how you have a pure 0-arity function that can nonetheless be dynamic (because dynamically defined).

Think of it like this: you have to have shared mutable state in order to really do anything with a computer program. But, shared mutable state is a huge source of problems. OOP deals with this by locking state in objects. Functional languages (like Elixir) tend to have immutable data structures, which is nice and clean and mathematical. But when the time comes to do things in the real world, you need mutable state. The purpose of having things like genservers and message passing is to give you a way to deal with mutable state while still remaining functional; instead of holding state in an object, you hold it in a genserver process. It’s actually kind of similar to OOP in that respect.

Do you need it for a simple LiveView webapp? Not at the beginning, no. As time goes by, though, it will help to know the underlying OTP voodoo because that’s what LiveView is built on. It will pay off because you’ll be able to figure things out more quickly as a result of understanding the underlying theory.

If, on the other hand, you’re doing stuff that actually requires knowledge of concurrency, e.g. a IOT app processes a gazillion messages per second, then yeah, knowledge of concurrency fundamentals is important.

Also Liked

amarandon

amarandon

Is the lambda term also placed into the new processes’s mailbox, like a startup message?

Let’s find out:

iex(12)> pid = spawn fn -> :timer.sleep(10_000); IO.puts("Messages I've got so far:"); flush(); end                                                        
#PID<0.130.0>
iex(13)> send pid, :hello                                                                          
:hello
Messages I've got so far:
:hello

So the answer is no :slight_smile:
flush is useful to know what’s in the inbox of the current process.

how important is understanding these low-level details for the day-to-day programming of Elixir?

If you’re going to do any concurrent programming with Elixir, it’s very important to go through these details to build a solid mental of how processes work in Elixir. If you just want to build a traditional webapp with Phoenix, you can probably ignore them for now.

lud

lud

I guess it depends on the labmda. If it has closures ( fn -> do_stuf(some_var) end), the they are copied into the new process. If the lambda is like &Module.fun/0 then it will not be copied because the new process can point directly to the code. If the lambda does not have closures ( fn -> do_stuff() end ) then I don’t know.

I think you must know how processes and messages work on the code level. On the VM level (that is, what is copied and what is not), is not so important for day-to-day programming. It will become more important when you will want to optimize (tipically by avoiding copying too much data between processes when sending messages, or spawning, or providing data to supervisor child specs).

Oh and welcome to Elixir !

Edit: sorry I did not understand your question correctly: some stuff is copied to the process memory space, but the lambda is not itself sent as a message in any case. The lamba reprensents the code that the process will run. If the lambda was sent as a message, what code would be run to receive it? (though it could be both executed and sent but that is not the case).

Regarding copying data between processes, the general rule of thumb is that you want to copy references to things (file names, IDs, names) instead of the data itself. But do not think about it too much, copying is fine if you just need it, or if it makes the code much simpler.

Where Next?

Popular in Questions Top

Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
pgiesin
This should be a simple problem but I just can’t seem to figure it out. I have a standalone Elixir app that won’t find the database. Dep...
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
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
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
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
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
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
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

Other popular topics Top

JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
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
William
I would like to know that is there any online source for learning Phoenix Framework for building E-Commerce Store? Any advantage on build...
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
AstonJ
You’re a programmer, so you don’t need spoon feeding with the conventional drivel about “this is an integer.” No. You need to know what’s...
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
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
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
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
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New

We're in Beta

About us Mission Statement