为远程节点上运行的进程获取PID [英] Getting PID for a process running on a remote node

查看:130
本文介绍了为远程节点上运行的进程获取PID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我们正在尝试使用Redis构建一个进程注册表(不打算使用现有的一旦grpoc,全球等,由于其他业务需要)作为数据存储(存储将用户id保存到节点| PID映射)。当一个进程启动时,它会以Redis的形式自己注册,形式为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通过解析远程节点上的pid:



在节点 a

 (@主机)1> pid_to_list(个体经营())。 
<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> id! my_test。
my_test

节点 a



 (@主机)2>齐平()。 
Shell得到my_test
ok

请注意,进程可能不会活着,所以在说话之前调用 erlang:monitor / 2 可能是个好主意。


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.

解决方案

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.

这篇关于为远程节点上运行的进程获取PID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆