[erlang-questions] Perform gen_event:call/3 inside Module:handle_event/2
Dmitry Kolesnikov
dmkolesnikov@REDACTED
Thu Oct 25 18:40:18 CEST 2012
Hello,
you could achieve dynamic behavior through add_handler/add_sup_handler and delete_handler.
The major point here you are re-using existed OTP function.
The requirement to accure all of the state of another event handler might be challenging.
I believe you need to
a) either execute callback in dedicated process spawn(fun() -> Callback(Payload) end)
b) re-factor your listener concept where event handlers independent. I would propose to follow-up this way.
- Dmitry
On Oct 25, 2012, at 7:01 PM, Son Tran-Nguyen wrote:
> >Could you please elaborate
> > * why do you have a list of callbacks within an event manager? Event Manager manages a list of event handlers for you?
>
> I want a dynamic way to define event handlers. Instead of statically defining event handlers in callback modules, I want some way for other processes to listen to when a gen_event manager receives event and act on that. By using a Fun as callback, it call inherits the scope of the process which calls it, adding more flexibility.
>
> > * why do you try to call event handler?
>
> Mostly to acquire some or all of the state of another event handler module.
>
>
> Sincerely,
>
>
> Son Tran-Nguyen
>
>
>
> On Thu, Oct 25, 2012 at 10:56 AM, Dmitry Kolesnikov <dmkolesnikov@REDACTED> wrote:
> Hello,
>
> In the nutshell, you are having deadlock at gen_event:call within event manager.
>
> Could you please elaborate
> * why do you have a list of callbacks within an event manager? Event Manager manages a list of event handlers for you?
> * why do you try to call event handler?
>
> I think in your case, a gen_server is required so that event_handler catches an event and passes it to gen_server that does a magical job for you.
>
> - Dmitry
>
> On Oct 25, 2012, at 6:40 PM, Son Tran-Nguyen wrote:
>
>> Hi,
>>
>> I am trying to make a listener tree using gen_event behavior.
>>
>> I have a listener module, which will have an event type and a
>> list of callbacks in its state. It will receive event in format
>> {EventName, Payload}, and call all the callbacks with Payload
>> if EventName matches the event type it handles:
>>
>> handle_event({EventName, Payload}, {EventName, Callbacks}) ->
>> [Callback(Payload) || Callback <- Callbacks],
>> {ok, {EventName, Callbacks}};
>>
>> In another module, I have a function
>>
>> on(EventName, EventMgr, Callback)
>>
>> Which allows me to add the listener callback module into a
>> gen_event manager EventMgr for EventName.
>>
>> The EventMgr also has other callback modules to hold other
>> states.
>>
>> If my Callback is like this
>>
>> Callback = fun(Payload) ->
>> io:format("About to run callback~n),
>> Prop = gen_event:call(EventMgr, other_module, some_property)
>> io:format("This never runs~n")
>> end
>>
>> The problem is gen_event:call is not running inside the Callback.
>> I don't event see the second io:format
>>
>> To think about it, the Callback is executed inside a handle_event
>> of the EventMgr, so I guess it's not in the right scope of something?
>>
>> It's also the same if inside a handle_event of some other module,
>> I do gen_event:call(self(), a_different_module, some_property), so
>> I take that this is not allowed.
>>
>> Is there anyway to do what I want to do?
>>
>> Sincerely,
>>
>>
>> Son Tran-Nguyen
>> _______________________________________________
>> erlang-questions mailing list
>> erlang-questions@REDACTED
>> http://erlang.org/mailman/listinfo/erlang-questions
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20121025/eee87b4d/attachment.htm>
More information about the erlang-questions
mailing list