[erlang-questions] Update Pid State on a Timer

James Aimonetti james@REDACTED
Mon Dec 5 06:39:17 CET 2016


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA256


zxq9 writes:

> On 2016年12月4日 日曜日 20:43:08 qp wrote:
>> 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?
>
> I assume the module name is `land`.
>
> If that is the case the handler for 'trigger' will call
>
> land:action(self(), {fight})
>
> which puts us inside action/2 above, which tries to make a call to self().
>
> Why would making a call to self() always fail with a timeout? Think about
> this carefully -- it is the most basic form of deadlock you can create.
>
> -Craig

This is more directly related to the problem; tired eyes on my part just
saw the return value - that will get you once you've ruminated on the
above reply :)

- --
James Aimonetti

Lead Systems Architect
"If Dialyzer don't care, I don't care"
2600HzPDX | http://2600hz.com
sip:james@REDACTED
tel:415.886.7905
irc:mc_ @ freenode
-----BEGIN PGP SIGNATURE-----

iQEcBAEBCAAGBQJYRP2FAAoJENTKa+JPXCVgZPEH/00KZao8ZbtDBpujkvpk+zew
SMl75rb28f058xY0jQCpaS67/+02iGEShvZ4Sz9voMyc47ti7PN/2BYDIuLXX8lf
Z2XgDxeIJeiasxqZMMDkMKg+Fiep5GbW7cf2H9tyri9xTXgb1mEGry9hkaFFR4/h
jX1PLZo8Z1Qg6ec41w5dWwSe47NvdX+uNecxruwl/HLv+54fRnOrh5TvNULrfFpp
zkWAojLb7O65Bn4EbtRj8Iw9wLxqaIl0Hyt2UIsPtpod7SSDBIbnfui4SR6j5bQ0
xEINW8MeKrEnTJ/yeyD9faIYUN8I9+868E2VKBC72ZSDLccwfJA5+Q50EU1h8sw=
=7jQ3
-----END PGP SIGNATURE-----



More information about the erlang-questions mailing list