anthonyl
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 ![]()
Trending in Questions
Hello!
Suppose you are building workflow (order / task / payment) processing system with the following requirements:
Each workflow con...
New
I’m aware of 2 such projects, are there others?
git: GitHub - team-alembic/realworld: A fullstack Phoenix LiveView application with back...
New
Hi everyone.
My team and I have been working on a fairly modest app based around video streaming and chat, but we’ve landed a customer t...
New
I really like the adapter patterns that ecto, nebulex, waffle, etc. use and would love find something similar for a key management servic...
New
Hello folks!
So at work, we are seeing some situations where we have to define some “fixed” strings that are used across the codebase in...
New
Hi,
we run different LLM models across a cluster, with each node loading a configurable subset. Model-specific queues let nodes consume ...
New
Hi all, I’m new here and would like to hear peoples views on my Elixir/Phoenix based game-engine! :smiley:
Mutonex gamedesign was finish...
New
Other Trending Topics
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
There has been a thread to discuss the Stack Overflow Developer Survey on this forum every year since 2018, so here’s yet another one for...
New
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself.
My main conc...
New
I love Elixir. It’s one of 2 programming languages I’ve ever fallen in love with.
But I don’t use it anymore.
Serverless was the promis...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #ai
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #blog-post
- #elixirconf-us
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #elixirconf-eu
- #api
- #forms
- #security
- #hex










Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
kip
:persistent_termis 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
:persistentterm 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_termso stream and store to your hearts content.anthonyl
Thx Kip.
Do you perhaps have a code sample of the correct use of persistent term? I have searched high and low and been unsuccessful in finding one.
kip
I don’t seem to have a good example immediately to hand. I do have an example, but arguably its not a good one. Anyway, you’re welcome to whatever value it may contribute:
Compile time determination to use
:persistent_termif its availableThe relevant macro that uses
:persistent_term.Really, its a very niche, almost experimental, use case to emit a logger warning only once. Probably not a good example, but the only one I have to hand at the moment.
anthonyl
Thx Kip,
Those examples are actually quite helpful. Please confirm if my understanding is correct. You basically use it as follows:
Create a unique key with the structure: modulename_key e.g mynamestore_firstname, mynamestore_lastname, mypricestore_socks
Save your value as:
`
:persistent_term.put(“mynamestore_firstname”, “Mark”)
:persistent_term.put(“mynamestore_lastname”, “Johnson”)
:persistent_term.put(“mypricestore_socks”, “25.00”)
etc…
Then to access you use:
first_name = :persistent_term.get(“mynamestore_firstname”)
last_name = :persistent_term.get(“mynamestore_lastname”)
sock_price = first_name = :persistent_term.get(“mypricestore_socks”)
Is that correct?
Nicd
Be sure to check out the performance implications of using persistent_term: persistent_term — OTP 29.0.2 (erts 17.0.2)
Namely updating and deleting terms may result in a global garbage collection pass that may or may not be acceptable on your use case.
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?
Nicd
Yes AFAIK that would be a good use case for persistent_term.
anthonyl
I have tried this code and it is not working:
defmodule LookupTable do
def build_lookup do
end
end
I get the following error:
** (UndefinedFunctionError) function :persisent_term.get/1
is undefined (module :persisent_term is not available)
:persisent_term.get(“global_name”)
kip
:persistent_termis only available on OTP 21.2 and later so I suspect you are on an older OTP release.anthonyl
Thx Kip,
I did upgrade both Erlang and Elixir to the latest release. :persistent_term does work in iex.