gen_server:call every X seconds
-
12-11-2019 - |
Question
The Status of the gen_server is a list and should be processed once every X seconds. Therefore, I need to execute handle_call({process},State) every X seconds.
What is the best way to have a handle_call executed every X seconds?
Solution
“Timer” module can solve your problem. For example, In otp hehaviour implemention module,
init([]) ->
timer:send_after(1000,self(),{create_log}), %<====== create an event after 1000ms
{ok, #state{id=1}}.
handle_info({create_log},#state{id=ID})-> %<=========handle the timer event
%io:format("handle info~n",[]),
New_id = ID + 1,
ls117_single_process_log:error("test log ~p~n",[New_id]),
timer:send_after(1000,self(),{create_log}), %<========restart timer
{noreply,#state{id=New_id}};
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow