gpt4 book ai didi

elixir - 等待 OTP 进程退出

转载 作者:行者123 更新时间:2023-12-04 12:15:27 25 4
gpt4 key购买 nike

假设您有一个 OTP 进程,您希望同步等待其完成(其中“完成”可能是正常退出或崩溃、停止等)。
进一步假设出于业务原因,您无法使用 Task.async/1 生成此进程。或相关 Task实用程序——它必须是一个不依赖于 Task.await/2 的“正常”进程.
有没有比简单地间歇性轮询更好的方法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 函数从调用进程监视给定进程。将此与 receive 结合使用,您可以对您的流程邮箱使用react。

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}}
注意接收块内的模式匹配,以确保消息不仅来自您的进程,而且来自特定的监控。

关于elixir - 等待 OTP 进程退出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68397504/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com