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.
- How do I assign a variable to a persistent term that I can access outside the module it was created in?
- 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 
Most Liked
kip
: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
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
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
Yes AFAIK that would be a good use case for persistent_term.
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?







