Erlang gen_event 不起作用
我尝试在 erlang 中编写简单的 gen_event 应用程序。
我的代码:
-module(test).
-behavior(gen_event).
-export([notify/0]).
%% API
-export([start_link/0, add_handler/0, stop/0]).
%% gen_event callbacks
-export([init/1, handle_event/2, handle_call/2,
handle_info/2, terminate/2, code_change/3]).
-define(SERVER, ?MODULE).
start_link() ->
gen_event:start_link({local, ?SERVER}).
add_handler() ->
gen_event:add_handler(?SERVER, ?MODULE, []).
stop() ->
gen_event:stop(?MODULE).
init([]) ->
%add_handler(),
{ok, null}.
handle_event(_Event, State) ->
{ok, State};
handle_event({test}, State) ->
io:format("Test"),
{ok, State}.
handle_call(_Request, State) ->
Reply = ok,
{ok, Reply, State}.
handle_info(_Info, State) ->
{ok, State}.
terminate(_Reason, _State) ->
ok.
code_change(_OldVsn, State, _Extra) ->
{ok, State}.
notify() ->
gen_event:notify(?SERVER, {test}).
当我尝试通知时,我期望函数handle_event({test},State)执行,但什么也没有发生。为什么?怎么了?
我在哪里可以看到 gen_event 的简单示例?
谢谢。
I try to write simple gen_event applcation in erlang.
My code:
-module(test).
-behavior(gen_event).
-export([notify/0]).
%% API
-export([start_link/0, add_handler/0, stop/0]).
%% gen_event callbacks
-export([init/1, handle_event/2, handle_call/2,
handle_info/2, terminate/2, code_change/3]).
-define(SERVER, ?MODULE).
start_link() ->
gen_event:start_link({local, ?SERVER}).
add_handler() ->
gen_event:add_handler(?SERVER, ?MODULE, []).
stop() ->
gen_event:stop(?MODULE).
init([]) ->
%add_handler(),
{ok, null}.
handle_event(_Event, State) ->
{ok, State};
handle_event({test}, State) ->
io:format("Test"),
{ok, State}.
handle_call(_Request, State) ->
Reply = ok,
{ok, Reply, State}.
handle_info(_Info, State) ->
{ok, State}.
terminate(_Reason, _State) ->
ok.
code_change(_OldVsn, State, _Extra) ->
{ok, State}.
notify() ->
gen_event:notify(?SERVER, {test}).
When i try notify i expect that function handle_event({test}, State) execute but nothing occurs. Why? What's wrong?
And where i can see simple example of gen_event?
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好吧,你已经颠倒了你的handle_event 子句。第一个将始终匹配,因此您不会到达 {test} 子句。
据我所知,最好的在线文档是 http://www.erlang.org /doc/design_principles/events.html。但我可以推荐《Erlang and OTP in action》这本书http://www.amazon。 com/Erlang-OTP-Action-Martin-Logan/dp/1933988789
Well, you have reversed your handle_event clauses. The first one will always match, so you won't reach the {test} clause.
As far as I know, the best online documentation is http://www.erlang.org/doc/design_principles/events.html. But I can recommend e.g. the book Erlang and OTP in action http://www.amazon.com/Erlang-OTP-Action-Martin-Logan/dp/1933988789
除了交换 @E Dominique 提到的子句之外,您实际上是否使用
test:add_handler()
添加了处理程序?调用test:start_link()
只是启动事件管理器,您需要通过添加处理程序来告诉它要做什么。即使处理程序代码与管理器位于同一模块中也是如此。即使作为sasl
一部分的alarm_handler
这样做,这通常也不是一个好的做法。执行此操作,您的代码可以在获取事件时编写
"Test"
。Apart from swapping the clauses as @E Dominique mentioned have you actually added a handler using
test:add_handler()
? The calltest:start_link()
just starts the event manager, you need to tell it what to do by adding handlers. This even if the handler code is in the same module as the manager. This is generally not good practice even ifalarm_handler
which is part ofsasl
does this.Doing this your code works writing
"Test"
when it gets an event.