Getting PID for a process running on a remote node

蹲街弑〆低调 提交于 2019-12-12 14:49:59

问题


I am new to Erlang and we are working on a small scale messaging server.

We are trying to build a process registry using Redis ( not planning to use existing once grpoc, global etc due to other business needs ) as a datastore ( store to hold user-id to "node | PID " mapping). When a process starts it register itself with Redis in the form user_id (key) and {node | pid } as value. ( pid is tored as string in redis)

example value inserted in redis are "user_abc", {one@mf, "0.37.0>"}

Now when I try to find PID for "user_abc" which is running in the cluster - i get {node and pid} as value on a remote node which in this case is {one@mf, "0.37.0>".

Question is how do we use {node, pid} details on remote node to connect to the process user_abc.

Thanks in advance for your help.


回答1:


You can get a "cluster wide" pid by parsing that pid on the remote node:

On node a:

(a@host)1> pid_to_list(self()).
"<0.39.0>"

On node b:

(b@host)1> Pid = rpc:call('a@host', erlang, list_to_pid, ["<0.39.0>"]).
<7101.99.0>
(b@host)2> Pid ! my_test.
my_test

On node a:

(a@host)2> flush().
Shell got my_test
ok

Note that the process might not be alive, so calling erlang:monitor/2 on it before talking to it might be a good idea.




回答2:


If you want to store erlang pid to redis, and want other node read it as a remote pid, use erlang:term_to_binary(Pid) to store, and use erlang:bianry_to_pid(PidBin) to read.

See this post:Erlang Pid



来源:https://stackoverflow.com/questions/32708758/getting-pid-for-a-process-running-on-a-remote-node

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!