[erlang-questions] Update Pid State on a Timer

AnthØny anthony@REDACTED
Mon Dec 5 15:25:06 CET 2016


Hi, thank you for your advice.
I have removed the reference to handleInfo - that was a mistake to have
that function in there.


I found a timer.beam i my current directory from an older example and that
is what caused the error!

Fully working timer example is here:

-module(counter).
-compile(export_all).

counter(State) ->
  receive
    {From, {add, Number}} ->
      NewNumber = State+Number,
      From ! {self(), NewNumber},
      counter(NewNumber);
    {From, {resolve}} ->
          From ! {self(), State},
          counter(0);
     terminate ->
       ok
   end.

add(Pid, Number) ->
  Pid ! {self(), {add, Number}},
  receive
    {Pid, Msg} -> Msg
  after 3000 ->
    timeout
  end.

resolve(Pid) ->
  Pid ! {self(), {resolve}},
  {ok, _Tref} = timer:apply_after(1000, ?MODULE, resolve, [Pid]),
  receive
    {Pid, Msg} -> Msg
  after 3000 ->
    timeout
  end.

start() ->
  Pid = spawn(?MODULE, counter, [0]),
  {ok, _Tref} = timer:apply_after(1000, ?MODULE, resolve, [Pid]),
  Pid.

On Mon, Dec 5, 2016 at 3:41 AM, Dmytro Lytovchenko <
dmytro.lytovchenko@REDACTED> wrote:

> In line 47 you do a gen_server call, possibly to your current process,
> which causes a deadlock and hence the timeout error. Change it to
> asynchronous gen_server:cast or a direct function call to break the
> deadlock or don't call gen_server functions from a gen_server in the same
> module.
>
> 2016-12-05 2:43 GMT+01:00 qp <quantumpotato@REDACTED>:
>
>>
>> I am trying to update my process's state on a 3 second timer.
>>
>> -define(INTERVAL, 3000).
>>
>> start_link() ->
>>     gen_server:start_link(?MODULE, [], []).
>>
>> action(Pid, Action) ->
>>   gen_server:call(Pid, Action).
>>
>> init([]) ->
>>   erlang:send_after(?INTERVAL, self(), trigger),
>>   {ok, temple:new()}.
>>
>> what I want to do is call this
>>
>> handle_call({fight}, _From, Temple) ->
>>   NewTemple = temple:fight(Temple),
>>   {reply, NewTemple, NewTemple};
>>
>> So I try
>>
>> handle_info(trigger, _State) ->
>>    land:action(self(), {fight}),
>>    erlang:send_after(?INTERVAL, self(), trigger);
>>
>> but I get
>>
>> =ERROR REPORT==== 4-Dec-2016::19:00:35 ===
>> ** Generic server <0.400.0> terminating
>> ** Last message in was trigger
>> ** When Server state == {{dict,0,16,16,8,80,48,
>>                                {[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],
>>                                 []},
>>                                {{[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],
>>                                  []}}},
>>                          []}
>> ** Reason for termination ==
>> ** {function_clause,[{land,terminate,
>>                            [{timeout,{gen_server,call,[<0.400.0>,{fight}]}},
>>                             {{dict,0,16,16,8,80,48,
>>                                    {[],[],[],[],[],[],[],[],[],[],[],[],[],[],
>>                                     [],[]},
>>                                    {{[],[],[],[],[],[],[],[],[],[],[],[],[],
>>                                      [],[],[]}}},
>>                              []}],
>>                            [{file,"src/land.erl"},{line,47}]}
>>
>>
>> What am I missing here?
>>
>>
>> _______________________________________________
>> 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/20161205/4ddb0fca/attachment.htm>


More information about the erlang-questions mailing list