Mukulch15

Mukulch15

What is the difference between process state and process dictionary?

I read about process dictionaries and how they introduce side effects but how are states different from process dictionaries ? Shouldn’t process states introduce the same issues that process dictionaries have ?

Marked As Solved

LostKobrakai

LostKobrakai

Yes both maintain state. The difference is in how the state is maintained. For process state the state data is explicitly passed as parameter in the function the process runs - most often called the process loop. This is essentially tail call based recursion and therefore functional at its core.

The process dictionary however doesn’t use recursion, but side effects to store data. I’m not even sure if the dictionary is also on the process heap like normal process state. In the end it’s something quite different than process state from an implementation perspective.

I’ll try to illustrate this with some code:

spawn(fn -> 
  fn -> 
    receive do
      msg -> msg
    end
  end
  |> Stream.repeatedly()
  |> Enum.reduce(initial_state, fn msg, state -> 
    next_state = handle_message(msg, state) 
    next_state
  end)
end)

This is kinda how a process loop works. State is continuouesly aggregated by a reducing function handling the last state and a received message from the outside. State is maintained by passing it as parameter to the next reduction. It’s never really stored somewhere, but part of the callstack. Most of the time you never see this part in your codebase though, because it’s handled by the various :gen behaviours we’re all using. E.g. in the above case a user would just need to provide handle_message, while the rest of the code wouldn’t need to change.

The process dictionary on the other hand is a “database”. It’s a key-value table you can put kv pairs in and then it’s just there, no matter what code runs on the process. Kinda like a process specific ets table.

11
Post #7

Also Liked

hauleth

hauleth

Process state is the value passed to you in the function argument, process dictionary is black magic that you probably should never touch on your own unlike you will really know what the hell is happening.

ityonemo

ityonemo

Don’t say “never”, it’s more like you should wait until you’re a senior dev that has seen how it’s used under the hood in elixir and understand fully why that choice is made where it is :slight_smile: and understand why your code is not that.

hauleth

hauleth

In my post s/unlike/until and it will make more sense. Unfortunately I cannot edit myself now :frowning:

Where Next?

Popular in Questions 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
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
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
logicmason
Hi there, I'm working through my first release with elixir/phoenix. I've built a release with distillery and found that it crashes when I...
New
Jim
As a follow up to my earlier question: I have the code compiling and running but not getting a successful login from the rest server. ...
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
beno
I will often find my self writing things similar to: case some_value do nil -> something() "" -> something() _ -> someth...
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
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum. ...
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
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: https://hexdocs.pm/ecto/Ecto.Schema.html#module-...
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
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
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
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New

We're in Beta

About us Mission Statement