stevensonmt

stevensonmt

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.

Showing Posts 1 to 4

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.

stevensonmt

stevensonmt OP

hmm. So Registry is like ETS with :private and no way to make it :public? It’s weird because the source for Registry all ETS tables are initiated with :public options. I might be better off dropping down to ETS in that case rather than adding a middleman.

I guess I also expected access to “bubble up” to parent processes which is clearly wrong. So what is the correct way to have sibling processes access a Registry? Do you need a Supervisor or GenServer as the parent process, that receives messages from children and updates the registry accordingly?

            Parent.Supervisor
                   _|___________
                  |   |        |
              MyReg   Child1  Child2
al2o3cr

al2o3cr

Cross-linking a useful analogy from another thread:

stevensonmt

stevensonmt OP

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

— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
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
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
asweet-confluent
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews