gpt4 book ai didi

erlang - gen_服务器 :call causes server to crash with a noproc

转载 作者:行者123 更新时间:2023-12-02 02:15:41 25 4
gpt4 key购买 nike

我有一个简单的服务器(如下)。我可以使用 cell_tracer:start_link() 创建服务器,这可以工作;我知道这一点是因为当我重复相同的调用时,我收到 already_started 错误。问题是,创建服务器后,对其使用 cell_tracker:get_cells() (或执行任何 gen_server:call(...))会导致服务器退出但有一个异常(exception):

** exception exit: {noproc,{gen_server,call,[cell_tracker,get_cells]}}
in function gen_server:call/2 (gen_server.erl, line 180)

我可以直接调用handle_call(get_cells...),并且得到预期的结果。

我不确定这里发生了什么。它没有给我提供太多可以使用的信息,而且我没有看到任何问题。我怎样才能进一步深入研究这个问题?

-module(cell_tracker).
-behavior(gen_server).
-export([start_link/0, stop/0]).
-export([add_cell/1, del_cell/1, get_cells/0]).
-export([init/1,
handle_cast/2,
handle_call/3,
terminate/2,
handle_info/2,
code_change/3]).

%% operational api
start_link() ->
gen_server:start_link({global, ?MODULE}, ?MODULE, [], []).

stop() ->
gen_server:cast(?MODULE, stop).

%% traking api

add_cell(Cell) ->
gen_server:call(?MODULE, { add_cell, Cell }).

del_cell(Cell) ->
gen_server:call(?MODULE, { del_cell, Cell }).

get_cells() ->
gen_server:call(?MODULE, get_cells).

%% gen_server callbacks

init(Coords) ->
{ok, Coords}.

handle_cast(stop, Coords) ->
{stop, normal, Coords}.

handle_call({add_cell, Cell}, _From, Cells) ->
{reply, ok, [Cell|Cells]};
handle_call({del_cell, Cell}, _From, Cells) ->
{reply, ok, Cells -- Cell};
handle_call(get_cells, _From, Cells) ->
{reply, Cells, Cells}.

terminate(unnormal, _State) -> ok.

handle_info(_,_) -> ok.
code_change(_,State,_) -> {ok, State}.

最佳答案

本地注册服务器

start_link() ->
gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).

或者如果您注册了 {global, ?MODULE} 转换,则调用 {global, ?MODULE}

参见gen_server

关于erlang - gen_服务器 :call causes server to crash with a noproc,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22290411/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com