查询Erlang进程的状态? [英] Query an Erlang process for its state?

查看:287
本文介绍了查询Erlang进程的状态?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  loop(State) - > 
收到
Msg - >
NewState = whatever(Msg),
loop(NewState)
end。

有没有办法用bif或跟踪来查询正在运行的进程的状态?由于崩溃消息说...当状态是...并显示崩溃的进程的状态时,我认为这将很容易,但我很失望,我没有找到一个bif来做到这一点。 p>

那么,我认为使用dbg模块的跟踪会做到这一点。不幸的是,我相信因为这些循环是尾调优化的,所以dbg只会捕获对函数的第一次调用。



任何解决方案?

解决方案

如果您的进程正在使用OTP ,则只需执行 sys:get_status(Pid)



您提到的错误消息由 SASL 显示。 SASL是 OTP 中的错误报告守护程序。



您在示例代码中引用的状态只是尾递归函数的参数。除了跟踪BIF之外,没有办法提取它。我想这不是生产代码中的正确解决方案,因为跟踪仅用于调试目的



正确,和行业测试,解决方案将在您的项目中广泛使用OTP。然后,您可以充分利用SASL错误报告, rb 模块收集这些报告, sys - 检查状态的运行OTP兼容进程, proc_lib - 使短命过程符合OTP标准等。


A common pattern in Erlang is the recursive loop that maintains state:

loop(State) ->
  receive
    Msg ->
      NewState = whatever(Msg),
      loop(NewState)
  end.

Is there any way to query the state of a running process with a bif or tracing or something? Since crash messages say "...when state was..." and show the crashed process's state, I thought this would be easy, but I was disappointed that I haven't been able to find a bif to do this.

So, then, I figured using the dbg module's tracing would do it. Unfortunately, I believe because these loops are tail call optimized, dbg will only capture the first call to the function.

Any solution?

解决方案

If your process is using OTP, it is enough to do sys:get_status(Pid).

The error message you mentions is displayed by SASL. SASL is an error reporting daemon in OTP.

The state you are referring in your example code is just an argument of tail recursive function. There is no way to extract it using anything except for tracing BIFs. I guess this would be not a proper solution in production code, since tracing is intended to be used only for debug purposes.

Proper, and industry tested, solution would be make extensive use of OTP in your project. Then you can take full advantage of SASL error reporting, rb module to collect these reports, sys - to inspect the state of the running OTP-compatible process, proc_lib - to make short-lived processes OTP-compliant, etc.

这篇关于查询Erlang进程的状态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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