If in iex, i just did GenServer.start_link(MyModule, [])
and forgot to assign the returned PID to a variable. How do I get that PID later in order to interact with that process?
Thanks
If in iex, i just did GenServer.start_link(MyModule, [])
and forgot to assign the returned PID to a variable. How do I get that PID later in order to interact with that process?
Thanks
From inside the process you can call self
…
If you’re in IEx, you can use the v
command to get values from the history.
Typing v
or v(-1)
will get you the last value, v(-2)
the value before that, and v(5)
the value from line iex(5)>
.
When calling start_link it returns a PID as you can see below;
iex> GenServer.start_link(MyModule, [])
{:ok, #PID<0.450.0>}
but when you’re not able to recover that into a variable there’s a way to transform the list/‘string’ format of that pid back into a real pid in the following way:
iex> pid = :erlang.list_to_pid('<0.450.0>')
#PID<0.450.0>
iex> Process.alive? pid
true
So, now you have multiple ways to recover that PID !
Or just pid(0, 450, 0)
is there a way to go into the process without the PID?
@benwilson512 @rjk @christopheradams all of your solutions are valid answers, thank you very much. Unfortunately I can only mark 1 answer as the solution.
In GenServer.call, the server parameter can be:
server :: pid | name | {atom, node}
pid - obviously won’t help you, but you can register the genserver by name globally:
You can e.g. use {:global, server_name_atom}
where server_name_atom is the name you used when calling Genserver.start_link/3
http://elixir-lang.org/docs/stable/elixir/GenServer.html#start_link/3
with the last parameter (options) being [{:name, {:global, server_name_atom}}]