结果未收到后收到

时间:2013-05-08 19:16:29

标签: erlang

给定一个功能:

%% @doc Retrieves client's state.
-spec(state(pid()) -> atom()).
state(Pid) when is_pid(Pid) ->
  case process_info(Pid) of
    undefined ->
      undefined;
    _Else ->
      Pid ! {state, self()},
      receive
        {state, State} ->
          State
      after
        1000 ->
          undefined
      end
  end.

它对于死亡的pids和活着的客户端的预期效果如下:

> client:state(A).
undefined
> client:state(Pid).
online

但是由于某些原因,如果进程Pid在1秒内没有回复他的状态,则返回Pid:

> client:state(self()).
<0.172.0>

我期待那里的'未定义'原子。 我该如何修复此代码?

1 个答案:

答案 0 :(得分:4)

这是因为您收到了您发送的邮件。您的函数正在shell进程上运行,并向自身发送{state, self()}消息。发送消息后,它会立即收到消息,并且函数以State结束,这是您发送的self()个pid。

我希望我不会太困惑。

相关问题