简体   繁体   中英

erlang spawn process error?

I'm following this tutorial and I tried to compile the event.erl and run it according to

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

But I cannot seem to get past step 8 because I get this error instead.

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>

The following is 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)].

EDIT: I tried to change the function loop by adding an argument from this

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

to this 

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

and it still compiles but it doesnt work, same error... I thought maybe the tuples didn't match. It then only evolves down to the argument to_go=[T|Next]} being incorrect.

I think the problem is that your loop/1 clause unpacks the #state parameter such that to_go is pattern-matched to a list (ie, to_go = [T|Next]). However, you pass in a 5 (to_go=5) which will fail to match [T | Next] therefore the loop/1 will not match and you will have no function clause which matches

loop(#state{server=self(), name="test", to_go=5})

This expression:

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

applies to an earlier version of the code, as it's being developed throughout the tutorial. If I'm reading it correctly, this should be the correct call:

19> event:start("Event", 0).

It is the same problem as in Erlang process event error . The start/2 , start_link/2 and time_to_go/1 functions expects a time argument of the form {{Year,Month,Day},{Hour,Minute,Second}} which is why they fail when you call them with 5 . The loop/1 function expects the to_go field of #state{} to be a list of integers which it interprets as seconds and will wait for the total time by doing a receive...after for each one. The goal of this server is to be able to wait for a longer period of time than Erlang directly can do in a receive...after .

The time_to_go/1 function is calculating the number of seconds between now and the date/time entered as argument and then breaking that down into a list of seconds of which none are greater than the maximum size receive...after can handle.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM