Naming a Process

The idea of referencing processes by their PIDs 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 node, 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, ...

Get Programming Elixir ≥ 1.6 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.