Elixir GenServer handle_call 回调 return 类型

Elixir GenServer handle_call callback return type

我正在实现一个 GenServer,但我很困惑。

我知道 handle_cast 是异步的,这意味着调用者不等待回复,我们 return 这样的元组:{:noreply, new_state}.

我注意到我们也可以 return 来自 handle_call 的相同元组。这是否意味着如果我有一个 handle_call 那个 return 和 {:noreply, new_state},它不会 return 除了同步之外什么都不会?调用者的流程将等待 GenServer.call 命令,然后在 handle_call 函数完成后继续?

调用者将阻塞等待您调用 GenServer.call 的回复 wheneverhandle_call 接受 {:noreply, state} 作为有效 return 的原因是允许您手动 reply/2(从任何地方)。

调用进程将阻塞等待 reply(以及根据您的设置超时等),同样如此。

handle_callback 的第二个参数是 from 引用,如在 handle_call(msg, from, state) 中,然后可以与 reply/2 一起使用。