kostonstyle

kostonstyle

What does name parameter mean

Hi all
I have following module that implement GenServer, the code is from book elixir in action.

defmodule Todo.DatabaseWorker do
  use GenServer

  def start_link(db_folder, worker_id) do
    IO.puts "Starting database worker #{worker_id}"

    GenServer.start_link(
      __MODULE__, db_folder,
      name: via_tuple(worker_id)
    )
  end

 defp via_tuple(worker_id) do
    {:via, Todo.ProcessRegistry, {:database_worker, worker_id}}
  end
end

What I do not understand is the name parameter in the start_link function. What does it mean, why the function via_tuple function return a tuple?

Thanks

Marked As Solved

hubertlepicki

hubertlepicki

Read this, I blogged about namig processes earlier today:

In your example, it’s slightly more complicted scenario, where you are using third party registry Todo.ProcessRegistry. Read this (after you read above):

https://m.alphasights.com/process-registry-in-elixir-a-practical-example-4500ee7c0dcc#.k4zo7xjho

Also Liked

Qqwy

Qqwy

TypeCheck Core Team

When normally (without using the name: option) creating a GenServer, the only way to refer to it, is by its Process ID (PID).

In most simple cases, this is good enough. However, in some cases it is annoying that there isn’t a different alias you can refer to, which means you’ll end up passing a list of PIDs around all over your application.

So instead, it is possible to register a process under a name, which is then stored globally (for the current node!) in Erlang. This means that one can refer to the process by using that name (as well as by its PID). This is often used when you only ever need a single process doing a certain kind. It is very common to have constructs like name: __MODULE__, which means that you only want a single GenServer to run using the code in the current module.

There are a few ways to specify a name other than an atom. @msambarino wrote a great explanation here that goes into more detail.

EDIT: Heh, @hubertlepicki was just a second quicker than I. Great blog post!

kostonstyle

kostonstyle

@hubertlepicki and @Qqwy thanks so much for help. The article process registry in very nice.

Thanks very much guys

Where Next?

Popular in Questions Top

lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
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
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
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
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New

Other popular topics Top

JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 55125 245
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
dogweather
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something… Haskell reminds me of Java, and e...
New
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
AstonJ
Posting this to see if we can make things easier for people to get into Neovim. If you use Neovim and have a favourite distro please let ...
New

We're in Beta

About us Mission Statement