Thanks, I definitely misunderstood the docs. And it seems I still don’t get how this is supposed to work…
When I spawn a task, it seems to work (using Task for simplicity)
iex(first@MacBook-Pro-9)2> :erlang.spawn_request :"second@MacBook-Pro-9", Task, :start, [fn ->
...(first@MacBook-Pro-9)2> IO.puts("started")
...(first@MacBook-Pro-9)2> Process.register(self(), :task)
...(first@MacBook-Pro-9)2> Process.sleep(:infinity)
...(first@MacBook-Pro-9)2> end],
...(first@MacBook-Pro-9)2> [:link]
#Reference<0.2708312481.1393819650.127085>
started
iex(first@MacBook-Pro-9)3> flush
{:spawn_reply, #Reference<0.2708312481.1393819650.127085>, :ok,
#PID<12056.121.0>}
On the other node, the process is not alive though
iex(second@MacBook-Pro-9)2> Process.alive? pid(0, 121, 0)
false
While the task is spawned under another PID
iex(second@MacBook-Pro-9)2> Process.whereis :task
#PID<0.122.0>
Another problem is that the :link
option doesn’t seem to work
iex(first@MacBook-Pro-9)4> Process.flag :trap_exit, true
iex(second@MacBook-Pro-9)3> Process.exit(v, :shutdown)
true
iex(second@MacBook-Pro-9)4> Process.whereis :task
nil
iex(first@MacBook-Pro-9)5> flush
:ok
EDIT: ok I see that now. When spawn_request
is called, some process is spawned on another node. If the link option is passed, the link is created to that process. Then, the spawned process spawns the actual process I want. So, to my understanding, this doesn’t establish a link to the GenServer/Task. Moreover, I need some additional logic to retrieve its PID.