gen_server的状态是一个列表,应该每X秒处理一次。因此,我需要每隔X秒执行一次handle_call({process},State)。
每隔X秒执行一次handle_call的最佳方法是什么?
答案 0 :(得分:2)
“计时器”模块可以解决您的问题。例如,在otp行为实现模块中,
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}};