stevensonmt

stevensonmt

Registry.update_value/3 error when called from Task but not from iex

I am trying to grow my skills a bit and exploring the Registry module. I have a custom registry set up as follows:

defmodule MyReg do 
  def start_link() do 
    Registry.start_link(keys: :unique, name: MyReg)
    Registry.register(MyReg, :default_key, MapSet.new())
  end

I want to iterate over a collection and store each item in the MapSet accessed by the :default_key but also use each item as another key with it’s own value.

# in the same module
def register(item, value) do 
  case Registry.register(MyReg, item, [value]) do 
    {:ok, _} ->
       # the item was not previously registered so add it to the collection of keys
       Registry.update_value(MyReg, :default_key, fn keys -> MapSet.put(keys, item) end)
    _ ->
      # if the item has already been registered, update it's values
       Registry.update_value(MyReg, item, fn vals -> [value | vals] end)
  end
end

When I run this manually from within iex -S mix it seems to work great. But when I try to run it as follows it fails at the update_value step.

MyReg.start_link()

collection
|> Task.async_stream(fn {item, value} -> 
        MyReg.register(item, value)
end)
|> Enum.to_list()
# [ok: :error, ok: :error, ...]
> Registry.lookup(MyReg, :default_key)
# MapSet.new()

I believe that the problem is that somehow the processes spawned by Task.async_stream do not have access to the registry created with MyReg.start_link, but I don’t know why.

After enumerating over the collection I can look at the contents of the registry and see that the initial start_link call was successful but that it was never modified after that.

Marked As Solved

mudasobwa

mudasobwa

Creator of Cure

As per documentation, this function updates the value for key for the current process in the unique registry.

Your example might be simplified to:

iex|💧|1 ▸ Registry.start_link(keys: :unique, name: MyReg)
iex|💧|2 ▸ Registry.register(MyReg, :default_key, :value)
iex|💧|3 ▸ spawn fn -> Registry.update_value(MyReg, :default_key, fn _ ->
...|💧|3 ▸   :new_value
...|💧|3 ▸ end) |> IO.inspect() end
:error

Registry.lookup/2 also returns a list of tuples {pid(), value}. You might turn your MyReg into GenServer and use its handle_cast/2/handle_call/2 to amend/query the backed registry.

Also Liked

al2o3cr

al2o3cr

Cross-linking a useful analogy from another thread:

stevensonmt

stevensonmt

Thanks, dropping back to ETS for this use case is probably the way to go.

Where Next?

Popular in Questions Top

sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
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
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a > b) do {:ok, "a"} end if (a < b) do {:ok, b} end if (a == b) do {:ok, "equa...
New
jaysoifer
Is there a way to rollback a specific migration and only that one (“skipping” all the other ones)? Would mix ecto.rollback -v 200809061...
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
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
yawaramin
In the Dialyzer docs ( dialyzer — OTP 29.0.2 (dialyzer 6.0.1) ), there is a way to turn off a specific warning for a function: -dialyzer...
New

Other popular topics Top

JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
AngeloChecked
What learn first? Rust or Elixir Hi Elixir community! I’m here because i want learn a new language. I’m a junior developer and mainly i ...
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
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 39297 209
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New

Latest on Elixir Forum

We're in Beta

About us Mission Statement