[erlang-questions] gen_server call and reply (Matthias Lang)

Ingela Anderton Andin <>
Tue Sep 18 13:09:57 CEST 2007


Hi again!

Humm ...  I might have been a bit quick in my last response. I agree 
that letting the gen_server:call
return a  reference and later sending a response  including that 
reference is
a good strategy when you have the situation that you want some job done 
and you want to
know when it is ready, but until then you can keep on doing other 
things. But if you
need to wait for the job to be done I do not think that you should  get 
an answer from gen_server:call/2 and then have do an explicit do receive 
on the  return value from gen_srever:call/2 .

Regards Ingela - OTP team


 wrote:
>> In the case of the long-running request, what I do instead is
>>
>>   {reply, {pending, Ref}, State}
>>
>> where Ref is a reference which is then included in a later message
>> sent the old-fashioned way. This amounts to more or less the same as
>> 'noreply', though it's one message more expensive and has the benefit
>> of making it easier to reason about timeouts, at least to my mind.
>>   
>>     
> I do not agree.  That changes the  semantics for the client. I think a 
> client that
> does a call shall hang until the reply is delivered (or is timed out)  
> that is the semantics of call otherwise you might as well use cast.  Of 
> course  for a long running request you do not want the server to hang 
> for the duration of the call and that is one of the  reasons why 
> gen_server:reply/2  exists. If it is an independent calculation the best 
> option is to spawn a new process that does the calculation and then 
> calls gen:server_reply/2 in the new process.  Otherwise you
> save "From" in the state and send the reply sometime later when you got 
> enough information from
> elsewhere to send the reply. Timeout can always be handled with the help 
> of erlang:send_after.
>
> Regards Ingela - OTP team
>
>
>   



More information about the erlang-questions mailing list