anthonyl

anthonyl

Correct Way to Use Persistent Term

Have been trying to figure out the correct way to code and use a persistent term, and am stuck.

  1. How do I assign a variable to a persistent term that I can access outside the module it was created in?
  2. How would I write the following code?

def build_store do

#Setup Persistent Term variable here

#Stream CSV File. I’m ok with this part using File.stream!() and parsing using split
|> Stream.map(fn x -> #Add to persistent term here# end) #This bit has me stumped

end

#Once term is created
#How do I read it here from another module?

Any assistance will be greatly appreciated :grinning:

Most Liked

kip

kip

ex_cldr Core Team

:persistent_term is VM-wide storage - so accessible from any module. This can create a bit of a challenge for naming so I typically use {module_name, key} as keys for persistent term.

The other consideration is balancing between a persistent storage and an amount of copying. Therefore I find myself using :persistent term when I can flatten the structure somewhat. For example, instead of storing a full map, I store each key of the map (and its value) in :persistent_term.

So to your example: Any module can access :persistent_term so stream and store to your hearts content.

NobbZ

NobbZ

As the docs said, all processes needs to be scanned to see which of them have a reference to the PT. There is no list of processes which have a reference, as the reference from the process might have been collected already via a regular GC, or the process might have sent copies of the reference to another process.

Keeping track of this would add additional complexity.

Nicd

Nicd

Be sure to check out the performance implications of using persistent_term: http://erlang.org/doc/man/persistent_term.html#description

Namely updating and deleting terms may result in a global garbage collection pass that may or may not be acceptable on your use case.

Nicd

Nicd

Yes AFAIK that would be a good use case for persistent_term.

anthonyl

anthonyl

Thx Nicd,

Yes, that is a very important consideration. The use case is a global lookup table that is only read once from a csv file and never updated. I believe it is ok in this use case. Is that correct?

Where Next?

Popular in Questions Top

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
yawaramin
In the Dialyzer docs ( http://erlang.org/doc/man/dialyzer.html#requesting-or-suppressing-warnings-in-source-files ), there is a way to tu...
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
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
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
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
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
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

Qqwy
Update: How to use the Blogs & Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3268 119930 1237
New
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
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
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
chrismccord
As promised, the first release candidate of Phoenix 1.3.0 is out! This release focuses on code generators with improved project structure...
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 42633 214
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
Nvim
Elixir appears to be a superior language to Python. I don’t see any advantage of Python over Elixir. Are there any?
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New

We're in Beta

About us Mission Statement