Erlang gen_server 投射错误的返回值
我尝试将消息投射到 gen_server:
gen_server:cast({global, ID}, {watchers}).
处理程序是:
handle_cast({watchers}, State) ->
case State#table_state.watchers of
[] ->
{reply, no_watchers, State};
_ ->
{reply, State#table_state.watchers, State}
end;
但是当我执行 gen_server:cast
时,gen_server 终止并出现错误:
=ERROR REPORT==== 29-Apr-2011::18:26:07 ===
** Generic server 1 terminating
** Last message in was {'$gen_cast',{watchers}}
** When Server state == {table_state,1,"1",11,[]}
** Reason for termination ==
** {bad_return_value,{reply, no_watchers, {table_state,3,"3",11,[]}}}
为什么我会得到 bad_return_value
?
I try to cast message to a gen_server:
gen_server:cast({global, ID}, {watchers}).
The handler is:
handle_cast({watchers}, State) ->
case State#table_state.watchers of
[] ->
{reply, no_watchers, State};
_ ->
{reply, State#table_state.watchers, State}
end;
But when I execute gen_server:cast
the gen_server terminates with error:
=ERROR REPORT==== 29-Apr-2011::18:26:07 ===
** Generic server 1 terminating
** Last message in was {'$gen_cast',{watchers}}
** When Server state == {table_state,1,"1",11,[]}
** Reason for termination ==
** {bad_return_value,{reply, no_watchers, {table_state,3,"3",11,[]}}}
Why do I get bad_return_value
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您无法使用强制类型转换进行回复(请参阅
gen_server
文档)。这就是转换异步消息而不是使用调用的全部要点。在您的情况下,您想返回回复,因此请使用
gen_server:call/2
。You cannot reply using cast (see
gen_server
documentation). That is the whole point of casting an asynchronous message instead of using call.In your case you want to return a reply, so use
gen_server:call/2
instead.