我一直在试图弄清楚如何在调用进程B中捕获或救援另一个进程A中的错误,该进程也杀死了进程A.如何通过在Elixir中调用进程来捕获或救援被调用进程的崩溃错误
这里是我的代码:
defmodule A do
def start_link do
GenServer.start_link(__MODULE__, :ok, name: :A)
end
def fun(fun_loving_person) do
GenServer.call(fun_loving_person, :have_fun)
end
def init(:ok) do
{:ok, %{}}
end
def handle_call(:have_fun, _from, state) do
raise "TooMuchFun"
{:reply, :ok, state}
end
end
defmodule B do
def start_link do
GenServer.start_link(__MODULE__, :ok, name: :B)
end
def spread_fun(fun_seeker) do
GenServer.call(:B, {:spread_fun, fun_seeker})
end
def init(:ok) do
{:ok, %{}}
end
def handle_call({:spread_fun, fun_seeker}, _from, state) do
result = A.fun(fun_seeker)
{:reply, result, state}
rescue
RuntimeError -> IO.puts "Too much fun rescued"
{:reply, :error, state}
end
end
{:ok, a} = A.start_link
{:ok, _b} = B.start_link
result = B.spread_fun(a)
IO.puts "#{inspect result}"
在模块B的handle_call
功能,我叫模块A的功能,它在任何情况下块rescue
出现错误与过程:A
。错误提出,但rescue
块未得到执行。
我是否错过了对一个进程崩溃如何影响另一个的基本理解?只有当错误发生在同一过程中时,才尝试/赶上或尝试/解救工作?我是否必须监视其他进程并将其退出?
我会感谢您的帮助。
谢谢。所以如果我理解你,抛出/抛出错误和捕获/救出它必须发生在同一个过程中? –
是的,必须在同一个进程代码 – Pouriya