【问题标题】:Erlang: turn pid into stringErlang:将pid变成字符串
【发布时间】:2015-03-17 11:26:11
【问题描述】:

我想把 erlang 中的 Pid 变成一个字符串,我该怎么做呢?

有没有办法对 Pid 进行模式匹配?

{Node, Index, Number} = <0.121.0>.

【问题讨论】:

  • 对于节点识别,请改用node/1
  • 你能解释一下你为什么想要这个吗?听起来您试图以错误的方式解决问题。

标签: erlang


【解决方案1】:
pid_tokens(Pid) ->
    PidStr = pid_to_list(Pid),
    PidStr1 = lists:sublist(PidStr, 2, length(PidStr)-2),
    [N, P1, P2] = [list_to_integer(T) || T <- string:tokens(PidStr1,[$.])],
    {N, P1, P2}.

这样的事情可能对你有用。其中 Pid 是您要匹配的进程 ID

【讨论】:

  • 不过要小心。 This BIF is intended for debugging and for use in the Erlang operating system. It should not be used in application programs.
  • @tkowal 知道有什么风险吗?
  • 你不应该依赖 Pid 内部结构,因为它可能会在没有警告的情况下发生变化。 pid_to_listlist_to_pid 在调试期间很方便,当您在 shell 中复制和粘贴 pid 时。唯一可能在实际程序中派上用场的时刻是检查进程是否在远程节点上。还有另一个功能:node(Pid).
  • 感谢@tkowal,当您说“因为它可能会在没有警告的情况下发生变化”时,这会是什么例子?
  • 我不知道 :) 任何事情!例如,它们可以显示为两个整数,而不是三个。或者分隔符可能从点更改为逗号 :) 或者有人可能想使用 64 位作为 pid,所以字符串表示中会有更多的数字......我认为目前没有任何改变它的计划。但是如果文档说“不要依赖它”,我宁愿在应用程序代码中避免它:)
猜你喜欢
  • 1970-01-01
  • 2018-06-19
  • 2011-05-11
  • 2010-12-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-13
相关资源
最近更新 更多