在您的 handle_call 函数中有第二个。参数来自 :: {pid(), tag()}
您可以在处理请求之前在handle_call 中调用monitor(process, FromPid),以便在您的erl_call 节点断开连接时收到DOWN 消息。但请注意,除非您生成一个单独的进程或使用带有 gen_server:reply() 的延迟回复,否则您将无法在当前的 handle_call 完成之前处理 DOWN 消息。
例如:这是我们的 handle_call 子句:
handle_call(test, {From, _}=X, State) ->
erlang:monitor(process, From),
spawn(fun() ->
timer:sleep(10000),
io:format("### DONE~n", []),
gen_server:reply(X, ok) end),
{noreply, State}.
接下来我们赶上DOWN:
handle_info(Info, State) ->
io:format("### Process died ~p~n", [Info]),
{noreply, State}.
接下来我从命令行生成 erl_call: erl_call -c 123 -n test -a 'gen_server call [a, test, infinity]'
然后按 Ctrl-C
10 秒后在 gen_server 控制台中,我看到:
### DONE
### Process died {'DOWN',#Ref<0.0.0.41>,process,<0.44.0>,normal}