0

我正在关注本教程,并尝试编译 event.erl 并根据

6> c(event).
{ok,event}
7> rr(event, state).
[state]
8> spawn(event, loop, [#state{server=self(), name="test", to_go=5}]).
<0.60.0>
9> flush().
ok
10> flush().
Shell got {done,"test"}
ok
11> Pid = spawn(event, loop, [#state{server=self(), name="test", to_go=500}]).
<0.64.0>
12> ReplyRef = make_ref().
#Ref<0.0.0.210>
13> Pid ! {self(), ReplyRef, cancel}.
{<0.50.0>,#Ref<0.0.0.210>,cancel}
14> flush().
Shell got {#Ref<0.0.0.210>,ok}
ok

但我似乎无法通过第 8 步,因为我收到了这个错误。

7> spawn(event, loop, [#state{server=self(), name="test", to_go=5}]).

=ERROR REPORT==== 14-Feb-2013::11:14:38 ===
Error in process <0.51.0> with exit value: {function_clause,[{event,loop,[{state,<0.32.0>,"test",5}],[{file,"event.erl"},{line,35}]}]}

<0.51.0>

以下是event.erl

-module(event).
-export([start/2, start_link/2, cancel/1]).
-export([init/3, loop/1]).
-record(state, {server,
                name="",
                to_go=0}).

%%% Public interface
start(EventName, DateTime) ->
    spawn(?MODULE, init, [self(), EventName, DateTime]).

start_link(EventName, DateTime) ->
    spawn_link(?MODULE, init, [self(), EventName, DateTime]).

cancel(Pid) ->
    %% Monitor in case the process is already dead
    Ref = erlang:monitor(process, Pid),
    Pid ! {self(), Ref, cancel},
    receive
        {Ref, ok} ->
            erlang:demonitor(Ref, [flush]),
            ok;
        {'DOWN', Ref, process, Pid, _Reason} ->
            ok
    end.

%%% Event's innards
init(Server, EventName, DateTime) ->
    loop(#state{server=Server,
                name=EventName,
                to_go=time_to_go(DateTime)}).

%% Loop uses a list for times in order to go around the ~49 days limit
%% on timeouts.
loop(S = #state{server=Server, to_go=[T|Next]}) ->
    receive
        {Server, Ref, cancel} ->
            Server ! {Ref, ok}
    after T*1000 ->
        if Next =:= [] ->
            Server ! {done, S#state.name};
           Next =/= [] ->
            loop(S#state{to_go=Next})
        end
    end.

%%% private functions
time_to_go(TimeOut={{_,_,_}, {_,_,_}}) ->
    Now = calendar:local_time(),
    ToGo = calendar:datetime_to_gregorian_seconds(TimeOut) -
           calendar:datetime_to_gregorian_seconds(Now),
    Secs = if ToGo > 0  -> ToGo;
              ToGo =< 0 -> 0
           end,
    normalize(Secs).

%% Because Erlang is limited to about 49 days (49*24*60*60*1000) in
%% milliseconds, the following function is used
normalize(N) ->
    Limit = 49*24*60*60,
    [N rem Limit | lists:duplicate(N div Limit, Limit)].

编辑:我试图通过添加一个参数来改变函数循环

loop(S = #state{server=Server, to_go=[T|Next]}) ->

to this 

loop(S = #state{server=Server,name=EventName, to_go=[T|Next]}) ->

它仍然可以编译,但它不起作用,同样的错误......我想也许元组不匹配。然后它只会演变为论点to_go=[T|Next]}不正确。

4

3 回答 3

2

我认为问题在于您的 loop/1 子句将 #state 参数解包,以便 to_go 与列表进行模式匹配(即 to_go = [T|Next])。但是,您传入一个 5 (to_go=5) 将无法匹配 [T | Next] 因此 loop/1 将不匹配,您将没有匹配的函数子句

loop(#state{server=self(), name="test", to_go=5})
于 2013-02-14T17:42:20.690 回答
1

这个表达式:

8> spawn(event, loop, [#state{server=self(), name="test", to_go=5}]).

适用于早期版本的代码,因为它正在整个教程中进行开发。如果我没看错,这应该是正确的调用:

19> event:start("Event", 0).
于 2013-02-14T18:11:33.107 回答
1

这与Erlang 进程事件错误中的问题相同。和函数需要一个时间参数,start/2这就是当你用 调用它们时它们会失败的start_link/2原因。该函数期望字段 of是一个整数列表,它将其解释为秒,并将通过对每个整数执行 a 来等待总时间。该服务器的目标是能够等待比 Erlang 直接在.time_to_go/1{{Year,Month,Day},{Hour,Minute,Second}}5loop/1to_go#state{}receive...afterreceive...after

time_to_go/1函数正在计算从现在到作为参数输入的日期/时间之间的秒数,然后将其分解为一个秒列表,其中没有一个大于receive...after可以处理的最大大小。

于 2013-02-14T23:03:25.683 回答