2012-02-24 14 views
2

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

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

答えて

2

"タイマー"モジュールで問題を解決できます。まったく同じセマンティックが、より軽量であり、タイマ・サーバが起動する必要はありませんsend_after/3`:例えば、OTP hehaviourのimplementionモジュールでは、

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}}; 
+4

私は 'アーランの使用をお勧めします。 –

関連する問題