【问题标题】:erlang - inspect mailbox messages once at a timeerlang - 一次检查邮箱消息
【发布时间】:2012-07-31 21:46:31
【问题描述】:

我正在尝试检查一个节点从其他节点接收到的消息,但是以flush() 以外的其他方式,因为消息大小相当大而且没有帮助。此外,我可以看到带有erlang:process_info(self(), messages_queue_len). 的消息,但我想要某种方式在某种变量中一次提取一条消息以用于调试目的。

【问题讨论】:

    标签: erlang


    【解决方案1】:

    您可能想看看 Erlang 中的 dbg 模块。

    启动跟踪器:

    dbg:tracer().
    

    跟踪进程(在本例中为 self())接收到 (r) 的所有消息:

    dbg:p(self(), r).
    

    更多信息here

    【讨论】:

      【解决方案2】:

      或者你可以使用:

      1> F = fun() -> receive X -> {message, X} after 0 -> no_message end end.
      #Fun<erl_eval.20.111823515>
      2> F().
      no_message
      3> self() ! foo.
      foo
      4> self() ! bar.
      bar
      5> F().
      {message, foo}
      6> F().
      {message, bar}
      

      ...防止阻塞

      【讨论】:

        【解决方案3】:

        receive 是用于从邮箱中获取消息的 erlang 原语。

        见:http://www.erlang.org/doc/getting_started/conc_prog.html#id2263965

        如果你只是想在 shell 中获取第一条消息进行调试,你可以尝试定义一个这样的 fun:

        1> self() ! foo.
        foo
        2> F = fun() -> receive X -> X end end.
        #Fun<erl_eval.20.67289768>
        3> F().
        foo
        

        【讨论】:

          猜你喜欢
          • 2012-11-17
          • 2014-08-26
          • 2011-01-04
          • 1970-01-01
          • 1970-01-01
          • 2019-02-28
          • 1970-01-01
          • 1970-01-01
          • 2012-08-12
          相关资源
          最近更新 更多