unregister不会停止进程。它只是删除了进程 id 和给定原子之间的绑定。
您需要记住该stop/0函数是在调用此函数的进程上下文中运行的,而不是在 gen_server 本身。实际上(几乎)与某个进程交互的唯一方法是向它发送消息。所以你可以stop/0像这样实现你的功能:
%% send stop message to `sts` server
stop() ->
gen_server:cast(sts, stop).
%% [...]
handle_cast( OtherCastMessages, State) ->
%% handel other cast messages ;
%% [...] ;
%% handle stop message
handle_cast( _Message = stop, State) ->
{stop,
_Reason = normal,
State}. % return result that stops server
%% [...]
terminate(_Reason = normal, State) ->
%% could do some cleanup in this callback
ok.
因此,要停止服务器,您必须从行为函数之一返回特殊元组。您可以在此处阅读有关此内容的更多信息。当然触发行为功能之一,您必须使用gen_server:castor向您的服务器发送消息gen_server:call(或者只是发送消息并使用 处理它handle_info)。使用什么是您的决定。最后terminate2被调用(无论哪个回调返回带有stop原子的元组),您可以在其中对您的状态进行一些清理。
当然,您可以在terminate回调中注销您的进程,但是当进程终止时,注销会自动处理。