let me know the concept of gen_event behaviour

Wiger Ulf <>
Mon Feb 10 21:07:13 CET 2003

Perhaps an example can illustrate gen_event. The behaviour basically works like this:


start_link(Name) ->
  spawn_link(fun() ->
               register(Name, self()),

add_handler(Name, Module, State) ->
   Name ! {add_handler, self(), Module, State},
      ok -> ok;

notify(Name, Event) ->
   Name ! {event, Event}.

event_loop(Modules) ->
      {add_handler, From, M, S} ->
         From ! ok,
      {event, E} ->
         Modules1 = call_modules(Modules, E),

call_modules([{M,S}|Modules], E) ->
   case catch M:handle_event(E, S) of
      {'EXIT', _} ->
         %% remove handler
         call_modules(Modules, E);
      {ok, NewS} ->
         [{M,NewS}|call_modules(Modules, E)]
call_modules([], _) ->

That is, you define a callback module if you want to handle events that are generated. Each event is just a message. Normally, you callback module would have a handle_event(Event, State) function that matches on a few events and simply ignores the rest.

As event handlers are called sequentially, they should do as little as possible in the handle_event/2 function. Sending a message to some server or state machine that then does the real work is probably a good idea.

  ----- Original Message ----- 
  From: Suresh S 
  Sent: den 10 februari 2003 17:43
  Subject: let me know the concept of gen_event behaviour 


  There is an Event manager can add Event handlers, This event handlers will handle particular event,

  Is every  Event is a Call back module ? if so  suppose there are 1000 calls processing in the applicaiton at once, then 1000 call events are generated, then if we use gen_event behaviour there will be 1000 call back modules active( generated )

  Please clarify this, let me know the concept of gen_event behaviours  

  Thanking u in advance.


  Suresh S


   Catch all the cricket action. Download Yahoo! Score tracker

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20030210/aec9ca83/attachment.html>

More information about the erlang-questions mailing list