<html>
<head>
<style><!--
.hmmessage P
{
margin:0px;
padding:0px
}
body.hmmessage
{
font-size: 12pt;
font-family:Calibri
}
--></style></head>
<body class='hmmessage'><div dir='ltr'>Thanks guys,<div><br></div><div>I guess I was under the assumption that a process could not even get any messages until init had returned and the pid was registered.<br><br><div>> Date: Mon, 27 Jul 2015 16:40:30 +0200<br>> From: essen@ninenines.eu<br>> To: mattevans123@hotmail.com; erlang-questions@erlang.org<br>> Subject: Re: [erlang-questions] gen_server and init<br>> <br>> This and sending yourself a message is a bad idea. It will usually work, <br>> until it doesn't, and you will have a very hard time figuring out why.<br>> <br>> Instead, start the process using proc_lib:spawn_link or <br>> proc_lib:start_link (depending on whether it needs to be synchronous or <br>> not), then perform your initialization (calling proc_lib:init_ack where <br>> appropriate), and finally calling gen_server:enter_loop.<br>> <br>> What this gives you is pretty much the ability to customize the <br>> initialization of your gen_server process.<br>> <br>> This is the correct way to do it. Your solution can fail if you receive <br>> a message. Sending yourself a message is subject to race conditions <br>> where you receive a message before you could init.<br>> <br>> On 07/27/2015 04:25 PM, Matthew Evans wrote:<br>> > Hi,<br>> ><br>> > We have used this pattern in gen_servers for a long time now:<br>> ><br>> > init(_) -><br>> >     %% Some stuff<br>> >     {ok, #state{}, 0}.<br>> ><br>> > %%%<br>> > %%%<br>> ><br>> > handle_info(timeout, State) -><br>> >      %% Init stuff<br>> >      {noreply,State}<br>> ><br>> > Basically the idea is to prevent init from blocking and to have init/1<br>> > return with a timeout of 0 causing an immediate timeout message to be<br>> > invoked in the handle_info.<br>> ><br>> > Here's what's odd. Sometimes this timeout does not fire. It doesn't<br>> > appear that any message is getting sent, but I would imagine that since<br>> > it's a registered gen_server there is no way that can happen.<br>> ><br>> > Does anyone have any ideas?<br>> ><br>> > We are running vsn 17.1, and ntp is enabled on the host (Linux).<br>> ><br>> > Thanks<br>> ><br>> ><br>> > _______________________________________________<br>> > erlang-questions mailing list<br>> > erlang-questions@erlang.org<br>> > http://erlang.org/mailman/listinfo/erlang-questions<br>> ><br>> <br>> -- <br>> Loïc Hoguin<br>> http://ninenines.eu<br>> Author of The Erlanger Playbook,<br>> A book about software development using Erlang<br></div></div>                                          </div></body>
</html>