【问题标题】:How to know the id of a node in erlang如何知道erlang中节点的id
【发布时间】:2012-08-27 01:45:15
【问题描述】:

当我们运行 node() 命令时,我们会得到节点的 pid。如果我们在同一个节点上,格式为 ,并且当从其他节点运行相同的命令时,我们会得到格式为 的结果。我想知道如何从同一节点上的 获取值 X。

【问题讨论】:

  • 实际上 node() 确实返回了一个代表 erlang 节点的原子(例如'yournode@yourhost')。也许你在谈论 self()。在这种情况下,一种简单的方法可能是使用 erlang:pid_to_list(Pid) 其中 Pid 是例如从 self() 获得的。

标签: erlang pid


【解决方案1】:

你是指pid的节点部分的整数值还是节点的name。对于 name 有 BIF node/1,它返回该 pid 所指节点的名称。所以

node(self())    ==> 'mynode@my_host.com'
node(RemotePid) ==> 'remote_node@remote_host.com'

它也适用于特定于节点的端口和引用。当前节点的第一个字段的值始终为0,远程节点的值会有所不同。对同一个远程节点的引用在不同节点上的值会有所不同。

注意 pid <X.Y.Z> 的表示实际上意味着 没有定义,所以不要过分依赖它。虽然不太可能改变。

【讨论】:

    【解决方案2】:

    这绝对没有意义。 <0.X.0> 是您的本地 Pid<D.X.0> - 分布式变体,其中 D 是节点的编号。 More information about Pid strcuture。但是D 对于不同的节点会有所不同。所以在本地你无法获得这些信息。

    当然,你可以实现特殊的 RPC 调用,它将返回给调用者他的(调用者的)分布式 Pid。但结果会因答案而异。为确保这一点,请尝试简单:

    启动三个不同的节点并将shell注册为self

    第一

    erl -sname one
    Erlang R15B01 (erts-5.9.1) [source] [64-bit] [smp:8:8] [async-threads:0] [hipe] [kernel-poll:false]
    
    Eshell V5.9.1  (abort with ^G)
    (one@Alexeys-MacBook-Pro)1> node().
    'one@Alexeys-MacBook-Pro'
    (one@Alexeys-MacBook-Pro)2> register(shell, self()).
    true
    

    第二

    erl -sname two
    Erlang R15B01 (erts-5.9.1) [source] [64-bit] [smp:8:8] [async-threads:0] [hipe] [kernel-poll:false]
    
    Eshell V5.9.1  (abort with ^G)
    (two@Alexeys-MacBook-Pro)1> node().
    'two@Alexeys-MacBook-Pro'
    (two@Alexeys-MacBook-Pro)2> register(shell, self()).
    true
    

    第三

    erl -sname three
    Erlang R15B01 (erts-5.9.1) [source] [64-bit] [smp:8:8] [async-threads:0] [hipe] [kernel-poll:false]
    
    Eshell V5.9.1  (abort with ^G)
    (three@Alexeys-MacBook-Pro)1> node().
    'three@Alexeys-MacBook-Pro'
    (three@Alexeys-MacBook-Pro)2> register(shell, self()).
    true
    

    现在返回节点one,尝试

    (one@Alexeys-MacBook-Pro)6> net_kernel:connect('two@Alexeys-MacBook-Pro').
    true
    (one@Alexeys-MacBook-Pro)7> net_kernel:connect('threeAlexeys-MacBook-Pro').
    true
    (one@Alexeys-MacBook-Pro)8> {shell, 'two@Alexeys-MacBook-Pro'} ! {hello, from, self()}.  
    {hello,from,<0.147.0>}
    (one@Alexeys-MacBook-Pro)82> {shell, 'three@Alexeys-MacBook-Pro'} ! {hello, from, self()}.  
    {hello,from,<0.147.0>}
    

    检查节点两个

    的结果
    (two@Alexeys-MacBook-Pro)3> flush().
    Shell got {hello,from,<6767.147.0>}
    ok
    

    (three@Alexeys-MacBook-Pro)3> flush().
    Shell got {hello,from,<6803.147.0>}
    ok
    

    如您所见,Pid 的第一部分不同:&lt;6767.147.0&gt;&lt;6803.147.0&gt;

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-02-22
      • 2019-03-15
      • 1970-01-01
      • 2013-03-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多