Mukulch15

Mukulch15

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 ?

Showing Posts 1 to 7

LostKobrakai

LostKobrakai

The process state is explicitly maintained by whatever code runs in a process. The process dictionary can be written to and later retrieved without the code running in the process passing it on.

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:

Mukulch15

Mukulch15 OP

But Agent actually allows us to use the state as a mutable variable right ? It can also be written and retrieved later.

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 #6
Mukulch15

Mukulch15 OP

Thanks a lot for the answers everyone.

— All posts loaded —

Where Next? Top

Trending in Questions Top

Blokh
Hey guys, I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly Do you guys have any suggestions what is the best prac...
New
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
Onor.io
I have what I’ve heard referred to as a “lookup table” in my database. This is a way of assigning codes to common values. One common lo...
New
jaybe78
Hello, I’m developing a online persistent chat system (what’s app) like using elixir/dynamodb/aws for a mobile app(flutter). The diffic...
New
Trolleger
What approach to take when sending live updates to “random” users Hi! I have a question, I have a little chat app, and when I create a DM...
New
matt-savvy
Anyone here using Honeybadger? My Honeybadger account is being overwhelmed with noise from some bots. Seeing a lot of Bandit.HTTPError...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New

Other Trending Topics Top

garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
wintermeyer
There are three potential reasons for members of this forum to have a look at https://vutuv.de You are tired or annoyed of LinkedIn. Yo...
New
webofbits
Aludel - LLM Evaluation Workbench Aludel is an embeddable Phoenix LiveView dashboard for evaluating and comparing LLM prompts across mult...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews