【发布时间】:2020-04-20 21:25:45
【问题描述】:
我正在使用 gen_server 行为,并试图了解如何从 timeout 中触发 handle_info/2,例如:
-module(server).
-export([init/1,handle_call/3,handle_info/2,terminate/2).
-export([start/0,stop/0]).
init(Data)->
{ok,33}.
start()->
gen_server:start_link(?MODULE,?MODULE,[]).
stop(Pid)->
gen_server:stop(Pid).
handle_call(Request,From,State)->
Return={reply,State,State,5000},
Return.
handle_info(Request,State)->
{stop,Reason,State}.
terminate(Reason,State)->
{ok,S}=file:file_open("D:/Erlang/Supervisor/err.txt",[read,write]),
io:format(S,"~s~n",[Reason]),
ok.
我想做的事:
我期待如果我启动服务器并且在5 秒内不使用gen_server:call/2(在我的情况下),那么handle_info 将被调用,这又会发出stop 从而调用@987654332 @。
我看到它不会以这种方式发生,实际上handle_info 根本没有被调用。
在this 等示例中,我看到timeout 设置在init/1 的返回值中。
我可以推断,只有在我初始化服务器时才会触发handle_info并且什么都不发出(对于 N 秒,cast 也不是 call)。如果是这样,为什么我可以在 return 和 handle_cast/2 和 handle_call/3 的 Timeout 中提供 Timeout ?
更新:
我试图获得以下功能:
- 如果在
X秒内没有发出call,则触发handle_info/2
- 如果在
Y秒内没有发出cast,则触发handle_info/2
我以为可以在return 的handle_call 和handle_cast 中设置这个超时时间:
{reply,Reply,State,X} //for call
{noreply,State,Y} //for cast
如果不是,这些超时是什么时候触发的,因为它们是returns?
【问题讨论】:
标签: server erlang timeout erlang-otp