Elixir - 获取主管下进程的所有 PID
Elixir - Get all PIDs for processes under a supervisor
我有一个 Supervisor,想知道在任何给定时间该 Supervisor 运行 下的所有进程。似乎应该有一种简单的方法来获取主管下或节点中所有进程的所有 PID、名称等,但我找不到任何东西。
关于如何做到这一点有什么建议吗?
您可以使用 Supervisor.which_children/1:
iex> Supervisor.which_children(MyApp.Supervisor)
[{MyApp.SubSupervisor, #PID<0.1695.0>, :supervisor, [MyApp.SubSupervisor]},
{MyApp.Endpoint, #PID<0.1686.0>, :supervisor, [MyApp.Endpoint]}]
Returns a list with information about all children of the given
supervisor.
Note that calling this function when supervising a large number of
children under low memory conditions can cause an out of memory
exception.
This function returns a list of {id, child, type, modules}
tuples,
where:
id
- as defined in the child specification
child
- the PID of the corresponding child process, :restarting
if the
process is about to be restarted, or :undefined
if there is no such
process
type
- :worker
or :supervisor
, as specified by the child specification
modules
- as specified by the child specification
由于提供了 type
和 pid
,如果需要,您可以递归获取子项以生成所有 pid 的列表。
我有一个 Supervisor,想知道在任何给定时间该 Supervisor 运行 下的所有进程。似乎应该有一种简单的方法来获取主管下或节点中所有进程的所有 PID、名称等,但我找不到任何东西。
关于如何做到这一点有什么建议吗?
您可以使用 Supervisor.which_children/1:
iex> Supervisor.which_children(MyApp.Supervisor)
[{MyApp.SubSupervisor, #PID<0.1695.0>, :supervisor, [MyApp.SubSupervisor]},
{MyApp.Endpoint, #PID<0.1686.0>, :supervisor, [MyApp.Endpoint]}]
Returns a list with information about all children of the given supervisor.
Note that calling this function when supervising a large number of children under low memory conditions can cause an out of memory exception.
This function returns a list of
{id, child, type, modules}
tuples, where:
id
- as defined in the child specification
child
- the PID of the corresponding child process,:restarting
if the process is about to be restarted, or:undefined
if there is no such process
type
-:worker
or:supervisor
, as specified by the child specification
modules
- as specified by the child specification
由于提供了 type
和 pid
,如果需要,您可以递归获取子项以生成所有 pid 的列表。