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, 1}], |
| :running, #PID<0.58.0>, [], |
| [header: 'Status for generic server seq', |
| data: [{'Status', :running}, |
| {'Parent', #PID<0.58.0>}, |
| {'Logged events', []}], |
| data: [{'State', "My current state is '102', and I'm happy"}]]]} |