我不应该直接调用gen_server:stop()吗?

时间:2016-02-17 20:01:18

标签: erlang otp gen-server

LYSE一书中,作者按如下方式处理服务器的终止:

%% Synchronous call
close_shop(Pid) -> gen_server:call(Pid, terminate).

handle_call(terminate, _From, Cats) ->
    {stop, normal, ok, Cats}.

terminate(normal, Cats) ->
    [io:format("~p was set free.~n",[C#cat.name]) || C <- Cats],
    ok.

因此它会从stop回调中返回handle_call值。

以下是我写作的方式:

close_shop(Pid) -> gen_server:stop(Pid).

terminate(_Reason, {Cats, Money}) ->
    io:format("Made $~w~n", [Money]),
    [io:format("~p was set free.~n",[C#cat.name]) || C <- Cats].

这不是一个好的做法,然后直接拨打gen_server:stop()吗?

1 个答案:

答案 0 :(得分:4)

直接致电gen_server:stop/1,3并不是一种坏习惯。它与LYSE中的示例几乎完全相同,但不从模块中调用handle_call/3。试试看吧。您甚至可以阅读源代码。