May 2018
Intermediate to advanced
412 pages
9h 3m
English
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, ... |
Read now
Unlock full access