获取远程节点上进程 运行 的 PID
Getting PID for a process running on a remote node
我是 Erlang 的新手,我们正在开发一个小型消息服务器。
我们正在尝试使用 Redis(由于其他业务需要,不打算使用现有的一次 grpoc、全局等)作为数据存储(用于保存用户 ID 到 "node | PID " 映射的存储)来构建进程注册表.当进程启动时,它以 user_id (key) 和 {node | pid } 作为值。 (pid 在 redis 中被撕成字符串)
redis 中插入的示例值是 "user_abc", {one@mf, "0.37.0>"}
现在,当我尝试为集群中的 "user_abc"(即 运行)查找 PID 时,我将 {node 和 pid} 作为远程节点上的值,在本例中为 {one@ mf, "0.37.0>".
问题是我们如何使用远程节点上的 {node, pid} 详细信息来连接到进程 user_abc。
在此先感谢您的帮助。
您可以通过解析远程节点上的 pid 获得 "cluster wide" pid:
在节点 a
上:
(a@host)1> pid_to_list(self()).
"<0.39.0>"
在节点 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
在节点 a
上:
(a@host)2> flush().
Shell got my_test
ok
请注意,该进程可能不存在,因此在与它对话之前对其调用 erlang:monitor/2
可能是个好主意。
如果想将erlang pid存储到redis,并且希望其他节点读取它作为远程pid,使用erlang:term_to_binary(Pid)
存储,并使用erlang:bianry_to_pid(PidBin)
阅读。
看到这个post:Erlang Pid
我是 Erlang 的新手,我们正在开发一个小型消息服务器。
我们正在尝试使用 Redis(由于其他业务需要,不打算使用现有的一次 grpoc、全局等)作为数据存储(用于保存用户 ID 到 "node | PID " 映射的存储)来构建进程注册表.当进程启动时,它以 user_id (key) 和 {node | pid } 作为值。 (pid 在 redis 中被撕成字符串)
redis 中插入的示例值是 "user_abc", {one@mf, "0.37.0>"}
现在,当我尝试为集群中的 "user_abc"(即 运行)查找 PID 时,我将 {node 和 pid} 作为远程节点上的值,在本例中为 {one@ mf, "0.37.0>".
问题是我们如何使用远程节点上的 {node, pid} 详细信息来连接到进程 user_abc。
在此先感谢您的帮助。
您可以通过解析远程节点上的 pid 获得 "cluster wide" pid:
在节点 a
上:
(a@host)1> pid_to_list(self()).
"<0.39.0>"
在节点 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
在节点 a
上:
(a@host)2> flush().
Shell got my_test
ok
请注意,该进程可能不存在,因此在与它对话之前对其调用 erlang:monitor/2
可能是个好主意。
如果想将erlang pid存储到redis,并且希望其他节点读取它作为远程pid,使用erlang:term_to_binary(Pid)
存储,并使用erlang:bianry_to_pid(PidBin)
阅读。
看到这个post:Erlang Pid