[erlang-questions] gen_server call and reply

Matthias Lang <>
Mon Sep 17 10:47:43 CEST 2007


Ulf "GH" Wiger writes:

 > There is no rule stating that the server must reply to requests
 > at all, so the gen_server module is wise not to buffer any information
 > about requests that have triggered a {noreply, _} response.

FWIW, I just took a look through our codebase to see how often we used
the 'noreply' variant. It never occurs. Why not?  

Why use 'noreply'? I can only really think of one reason---you have a
relatively long-running request but don't want to block the gen_server
while it runs. Maybe there's also some case where you can avoid a
deadlock, but I haven't thought about that.

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.

Matthias



More information about the erlang-questions mailing list