如何找到OTP流程的主管? [英] How to find the supervisor of an OTP process?

查看:106
本文介绍了如何找到OTP流程的主管?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可以让OTP进程找到其主管的pid的功能?

解决方案

数据被隐藏$ {code>'$ ancestors' $ {code> proc_lib 中的任何进程产生的进程字典b
$ b

  1> proc_lib:spawn(fun() - > timer:sleep(infinity)end)。 
< 0.33.0>
2> I(0,33,0)。
[{current_function,{timer,sleep,1}},
{initial_call,{proc_lib,init_p,3}},
{status,waiting},
{message_queue_len, 0},
{messages,[]},
{links,[]},
{dictionary,[{'$ ancestors',[< 0.31.0> b $ b {'$ initial_call',{erl_eval,' - expr / 5-fun-1 - ',0}}]},
{trap_exit,false},
{error_handler,error_handler}
{priority,normal},
{group_leader,< 0.24.0>},
{total_heap_size,233},
{heap_size,233},
{ stack_size,6},
{reduced,62},
{garbage_collection,[{min_bin_vheap_size,46368},
{min_heap_size,233},
{fullsweep_after,65535},
{minor_gcs,0}]},
{suspending,[]}]

这里我们感兴趣的是 {dictionary,[{'$ ancestors',[< 0.31.0>]},



请注意,这是您很少有任何理由使用自己的东西。据我所知,它主要用于处理监督树中的清除终止,而不是内省任何代码。小心处理。



一个更干净的方式来做事情,而不会混淆OTP的明智内疚就是让主管传递自己的pid作为过程的参数启动时。对于读取您的代码的人来说,这点应该更为困惑。


Are there functions which would allow an OTP process to find the pid of its supervisor?

解决方案

The data is hidden in the process dictionary (of any process spawned with proc_lib) under the entry '$ancestors':

1> proc_lib:spawn(fun() -> timer:sleep(infinity) end).
<0.33.0>
2> i(0,33,0).
[{current_function,{timer,sleep,1}},
 {initial_call,{proc_lib,init_p,3}},
 {status,waiting},
 {message_queue_len,0},
 {messages,[]},
 {links,[]},
 {dictionary,[{'$ancestors',[<0.31.0>]},
              {'$initial_call',{erl_eval,'-expr/5-fun-1-',0}}]},
 {trap_exit,false},
 {error_handler,error_handler},
 {priority,normal},
 {group_leader,<0.24.0>},
 {total_heap_size,233},
 {heap_size,233},
 {stack_size,6},
 {reductions,62},
 {garbage_collection,[{min_bin_vheap_size,46368},
                      {min_heap_size,233},
                      {fullsweep_after,65535},
                      {minor_gcs,0}]},
 {suspending,[]}]

Here the line that interests us is {dictionary,[{'$ancestors',[<0.31.0>]},.

Note that this is the kind of stuff you should rarely have any reason to use yourself. As far as I know, it's mostly used to handle clean termination in supervision trees rather than introspection for whatever code you have. Handle with care.

A cleaner way to do things without messing with OTP's sensible innards would be to have the supervisor pass its own pid as an argument to the process when starting it. This should be far less confusing for the people who'll read your code.

这篇关于如何找到OTP流程的主管?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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