等待 OTP 进程退出

Await an OTP process to exit

假设您有一个 OTP 进程,您希望同步等待其完成(其中“完成”可能是正常退出或崩溃、停止等)。

进一步假设,出于业务原因,您不能使用 Task.async/1 或相关 Task 实用程序生成此进程——它必须是不依赖于 [=13= 的“正常”进程].

有没有比简单地间歇性轮询 Process.alive?/1 更好的方法?这让我印象深刻,因为它可能有一个既定的模式,但对于我的生活我找不到它。

def await(pid, timeout) when timeout <= 0 do
  if Process.alive?(pid) do
    Process.exit(pid, :kill) # (or however you want to handle timeouts)
  end
end

@await_sleep_ms 500
def await(pid, timeout) do
  # Is there a better way to do this?
  if Process.alive?(pid) do
    :timer.sleep(@await_sleep_ms)
    await(pid, subtract_timeout(timeout, @await_sleep_ms))
  end
end

看起来 Process.monitor/1 可能就是您要找的东西?来自文档的示例:

pid = spawn(fn -> 1 + 2 end)
#=> #PID<0.118.0>
Process.monitor(pid)
#=> #Reference<0.906660723.3006791681.40191>
Process.exit(pid, :kill)
#=> true
receive do
  msg -> msg
end
#=> {:DOWN, #Reference<0.906660723.3006791681.40191>, :process, #PID<0.118.0>, :noproc}

Process.monitor/1 function monitors the given process from the calling process. Combining this with receive,你可以回复你的进程邮箱。

defmodule Await do
  def spawn do
    Kernel.spawn(fn -> :timer.sleep(2000) end)
  end
  
  def await_down(pid) do
    ref = Process.monitor(pid)
    receive do
      {:DOWN, ^ref, :process, ^pid, _reason} = message -> {:ok, message}
    after
      3000 -> {:error, :timeout}
    end
  end
end

pid = Await.spawn()
# => #PID<0.332.0>

Await.await_down(pid)
# => {:ok, {:DOWN, #Reference<0.4083290149.3661365255.180715>, :process, #PID<0.332.0>, :normal}}

注意接收块内的模式匹配,以确保消息不仅来自您的进程,而且来自特定的监控。