gen_event中的handler和supervised handler
呃,在gen_event中有兩個添加handler的方法
gen_event:add_handler/3 gen_event:add_sup_handler/3
一開始總是有些迷惑兩者的區(qū)別,今天查看了gen_event源碼,總算弄清兩者的區(qū)別。
add_handler添加的只是把gen_event作為容器,僅僅在適當(dāng)?shù)臅r間調(diào)用模塊的回調(diào)函數(shù)。(后面我描述為normal handler)
而add_sup_hander除了在適當(dāng)?shù)臅r間調(diào)用模塊的回調(diào)函數(shù)以外,同時link到調(diào)用該函數(shù)的進(jìn)程。(后面我描述為supervised handler)
?
首先看看在gen_event中描述handler的record
-record(handler, {module :: atom(),id = false,state,supervised = false :: 'false' | pid()}). supervised字段用來區(qū)分是那種類型的handler,
首先我們查看一下,添加兩種類型的handler實際做了什么事。
%% server_add_handler(Handler, Args, MSL) -> {Ret, MSL'}.
%% where MSL = [#handler{}]
%% Ret goes to the top level MSL' is the new internal state of the
%% event handlerserver_add_handler({Mod,Id}, Args, MSL) ->Handler = #handler{module = Mod,id = Id},server_add_handler(Mod, Handler, Args, MSL);
server_add_handler(Mod, Args, MSL) -> Handler = #handler{module = Mod},server_add_handler(Mod, Handler, Args, MSL).server_add_handler(Mod, Handler, Args, MSL) ->case catch Mod:init(Args) of{ok, State} ->{false, ok, [Handler#handler{state = State}|MSL]};{ok, State, hibernate} ->{true, ok, [Handler#handler{state = State}|MSL]};Other ->{false, Other, MSL}end.%% Set up a link to the supervising process.
%% (Ought to be unidirected links here, Erl5.0 !!)
%% NOTE: This link will not be removed then the
%% handler is removed in case another handler has
%% own link to this process.
server_add_sup_handler({Mod,Id}, Args, MSL, Parent) ->link(Parent),Handler = #handler{module = Mod,id = Id,supervised = Parent},server_add_handler(Mod, Handler, Args, MSL);
server_add_sup_handler(Mod, Args, MSL, Parent) -> link(Parent),Handler = #handler{module = Mod,supervised = Parent},server_add_handler(Mod, Handler, Args, MSL). 從上面的代碼可以看到,如果添加的是normal handler,supervised保持為默認(rèn)的'false',
如果為supervised handler,則存放調(diào)用改函數(shù)時候的進(jìn)程id。
最后,都調(diào)用了Mod:init/1函數(shù),將生成handler添加進(jìn)handlers 列表中。
?
接下來,我們再看看當(dāng)gen_event接受到'EXIT'消息時候,做的處理。
%% First terminate the supervised (if exists) handlers and %% then inform other handlers. %% We do not know if any handler really is interested but it %% may be so ! handle_exit(From, Reason, MSL, SName) ->MSL1 = terminate_supervised(From, Reason, MSL, SName),{_,MSL2}=server_notify({'EXIT', From, Reason}, handle_info, MSL1, SName),MSL2.terminate_supervised(Pid, Reason, MSL, SName) ->F = fun(Ha) when Ha#handler.supervised =:= Pid ->do_terminate(Ha#handler.module,Ha,{stop,Reason},Ha#handler.state,{parent_terminated, {Pid,Reason}},SName,shutdown),false;(_) ->trueend,lists:filter(F, MSL).do_terminate(Mod, Handler, Args, State, LastIn, SName, Reason) ->Res = (catch Mod:terminate(Args, State)),report_terminate(Handler, Reason, Args, State, LastIn, SName, Res),Res.
因為在添加supervised ?handler時候,我們link到了調(diào)用者進(jìn)程。
所以當(dāng)收到'EXIT'通知消息的時候,我們必須判斷是否為supervised 進(jìn)程的終止消息,如果是的話,需要刪除handler列表中的該supervised handler。
同時調(diào)用Mod:terminate/2函數(shù),并通知其他的handlers。
?
當(dāng)停止gen_event時候:
terminate_server(Reason, Parent, MSL, ServerName) ->stop_handlers(MSL, ServerName),do_unlink(Parent, MSL),exit(Reason).%% unlink the supervisor process of all supervised handlers. %% We do not want a handler supervisor to EXIT due to the %% termination of the event manager (server). %% Do not unlink Parent ! do_unlink(Parent, MSL) ->lists:foreach(fun(Handler) when Handler#handler.supervised =:= Parent ->true;(Handler) when is_pid(Handler#handler.supervised) ->unlink(Handler#handler.supervised),true;(_) ->trueend,MSL).
我們也需要判斷supervised handler,對進(jìn)程進(jìn)行unlink,當(dāng)gen_event進(jìn)程結(jié)束時候,并不會強(qiáng)制supervisored handler進(jìn)程結(jié)束。
?
而在下面兩個函數(shù)調(diào)用時候:
gen_event:delete_handler/3 gen_event:swap_sup_handler/3
除了正常的邏輯,同時向supervisored handler的宿主進(jìn)程發(fā)送{gen_event_EXIT,Handler,Reason},supervised handler進(jìn)程可以依此消息來做出處理。。。
?
轉(zhuǎn)載于:https://www.cnblogs.com/star-star/archive/2013/05/23/3095203.html
總結(jié)
以上是生活随笔為你收集整理的gen_event中的handler和supervised handler的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 云开头的成语有哪些啊?
- 下一篇: clientcontainerThrif