<div dir="ltr"><div>Hi,</div><div><br></div><div>Firstly - I think that your use case is not quite what global is made for.</div><div><br></div><div>In my company's codebase we have a behaviour called tracked_supervisor which does almost exactly what you're describing - it has a simple_one_for_one supervisor for supervising some children where each child has a unique id (which is just a term) and there's also a tracker module (a gen_server) which is responsible for starting the children and keeping the pid->name mapping in an ETS table. Our behaviour does not do the reverse lookup (pid -> name) but it would be trivial to add such a feature.</div><div><br></div><div>There's also a library called Director that seems to do something similar - <a href="https://github.com/pouriya/director">https://github.com/pouriya/director</a>, however it round-trips a process for every lookup which may be unacceptable performance-wise (lookups in both global and aforementioned tracked_supervisor are direct ETS reads).</div><div><br></div><div>Last but not least, I'd recommend that you look into the global module itself (lib/kernel/src/global.erl in the OTP repo). It's not complicated and you can see what the shape of the data in its ETS table is. With that knowledge you could use ets:match to get the name of your pid directly from global's ETS table. However, this is NOT something I would recommend unless you're _certain_ you know what you're doing - I'm sure that the OTP team makes no guarantee that internal data structures retain their shape so code relying on such details may break one day with no warning whatsoever! I only mention this to encourage people to not be afraid to look under the covers of the OTP platform because a number of often used modules are implemented in plain Erlang so it's not at all difficult to understand how they work.</div><div><br></div><div>Hope this helps!</div><div><br></div><div>Karl<br></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Fri, 8 May 2020 at 09:14, Nato <<a href="mailto:nato@natoviolin.com">nato@natoviolin.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">Hi list<br>
<br>
I have a gen_statem where I am creating simple_one_for_one<br>
children and using the global+term strategy. So each child's<br>
key, as it were, is a term. I have read the manuals for<br>
global, supervisor, sys, and can't seem to find a way to<br>
reverse lookup a child's id, via its known Pid. I suppose<br>
I was expecting global to have a way to do this... something<br>
I'm missing, no doubt.<br>
<br>
Right now, via a collect on `supervisor:which_children(foo_sup)`<br>
I can access the child Pids, and in an unsavory way, for<br>
each, matching on a whopper term via `sys:get_status/1` <br>
I can extract the Id in a `header' tuple. Is there<br>
another way?<br>
<br>
<br>
<br>
--<br>
Nato<br>
</blockquote></div>