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

_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
Werner
Hi, I’m using Ubuntu 18.04 and after updating to OTP-24.0 yesterday i have this warning when I run “mix local.hex”: 14:57:30.512 [warn] ...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
Fl4m3Ph03n1x
Background Let’s assume I have a typical GenServer that receives messages as requests, does some operation in a DB and returns responses....
New
chewm
Hi guys, nice to meet you to the whole forum, I’m new here, I’m trying to configure visual studio code for elixir, right now the intellis...
New
Exadra37
Sometimes I want to check if the input into a function is not a blank string. My first approach: defmodule Example do def do_stuff(s...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

Other popular topics Top

shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
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
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
sergio
I couldn’t find any guides that worked well with Phoenix 1.6.0 and esbuild. I hope this helps people test the waters and eases you into t...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
mgjohns61585
Could someone help me? I'm making my first elixir program, number guessing game. I can't figure out how to convert the user's guess from ...
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list....
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New

We're in Beta

About us Mission Statement