【问题标题】:Erlang gen_server cast bad return valueErlang gen_server 转换错误的返回值
【发布时间】:2011-08-15 11:31:03
【问题描述】:

我尝试将消息投射到 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

【问题讨论】:

  • 附带说明,您不必发送 {watchers}(元组中的原子),只需发送 watchers 原子作为消息就足够了。

标签: erlang gen-server


【解决方案1】:

您无法使用 cast 回复(请参阅gen_server documentation)。这就是投射异步消息而不是使用调用的全部意义。

在您的情况下,您想要回复,所以请改用gen_server:call/2

【讨论】:

  • 所以handle_cast 应该返回{noreply,State}。使用gen_server:callhandle_call 进行返回值的同步调用。
猜你喜欢
  • 2019-01-29
  • 2019-07-17
  • 2011-02-06
  • 2016-04-03
  • 2011-10-08
  • 2011-08-09
  • 2014-11-09
  • 2016-10-19
  • 2016-08-09
相关资源
最近更新 更多