3

gen_server のステータスはリストであり、X 秒ごとに 1 回処理する必要があります。したがって、X 秒ごとに handle_call({process},State) を実行する必要があります。

X秒ごとにhandle_callを実行する最良の方法は何ですか?

4

1 に答える 1

2

「タイマー」モジュールはあなたの問題を解決することができます。たとえば、otp hehaviour実装モジュールでは、

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}}; 
于 2012-02-24T13:13:31.737 に答える