O'Reilly logo

Programming Elixir by Dave Thomas

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Naming a Process

The idea of referencing processes by their PID gets old quickly. Fortunately, there are a number of alternatives.

The simplest is local naming. We assign a name that is unique for all OTP processes on our server, and then we use that name instead of the PID whenever we reference it. To create a locally named process, we use the name: option when we start the server:

*
iex>​ { :ok, pid } = GenServer.start_link(Sequence.Server, 100, name: :seq)
 
{:ok,​#PID<0.58.0>}​​​
 
iex>​ GenServer.call(:seq, :next_number)
 
100​​
 
iex>​ GenServer.call(:seq, :next_number)
 
101​​
 
iex>​ :sys.get_status :seq
 
{:status, ​#PID<0.69.0>, {:module, :gen_server},
 
[[​"$ancestors"​: [​#PID<0.58.0>],
 
"$initial_call"​: {Sequence.Server, :init, ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required