Jef*_*son 5 erlang erlang-otp gen-fsm
通常,如果我想让Erlang进程超时,我会使用以下结构:
receive
Msg -> ok; %% handle message
after 60000 ->
%% Handle timeout and exit
end.
Run Code Online (Sandbox Code Playgroud)
在gen_fsm等OTP服务器中是否有类似的机制?我将使用我的应用程序为每个活动会话生成gen_fsm,并且如果在收到消息后超过了不活动的超时值,则希望退出它们.
如果需要,我可以编写自己的自定义进程,但如果可能的话,我更愿意使用gen_fsm.
Jef*_*son 11
我挖了一些,找到了我自己的问题的答案.
在消息处理程序"Result"中有一个可选的第四个参数,您可以使用它是一个超时.
所以:
some_fsm_state({set, Val}, State) ->
NewState = do(Val, State),
{next_state, another_fsm_state, NewState, 5000};
another_fsm_state(timeout, State) ->
handle_timeout(State).
another_fsm_state({set, Val}, State) ->
%% more code that handles this state.
Run Code Online (Sandbox Code Playgroud)
一旦调用了some_fsm_state,它就会转换到下一个状态"another_fsm_state",超时为5000ms.如果在5000ms内没有收到新消息,则调用another_fsm_state(timeout,State).
聪明的OTP程序员.:)
应该注意的是,结果元组中的第四个元素可以是休眠的.有关更多信息,请参阅Erlang文档.