Erlang gen_server init

參考文章 http://youthyblog.com/2015/07/31/erlang-question-gen-server-and-init/

很幸運能看到這篇文章,從而獲得一種新的寫法。平常都是用 timeout =0 ,做一些耗時或者初始化操作。

僞代碼:

start_link(Args) ->
    proc_lib:start_link(?MODULE, init, [self(),Args]).

init([Parent,Args]) ->
    register(?MODULE, self()),
    proc_lib:init_ack(Parent, {ok, self()}),
    %%do_initialization
    gen_server:enter_loop(?MODULE, [], Args).

相關文章:

https://pdincau.wordpress.com/2013/04/17/how-to-handle-configuration-in-init1-function-without-slowing-down-your-erlang-supervisor-startup/

http://stackoverflow.com/questions/14648304/is-handle-info-guaranteed-to-executed-first-in-a-process-after-init-with-timeout

發佈了79 篇原創文章 · 獲贊 126 · 訪問量 29萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章