[erlang-questions] gen_server call and reply

Hakan Mattsson hakan@REDACTED
Mon Sep 17 20:10:56 CEST 2007

On Mon, 17 Sep 2007, Matthias Lang wrote:

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

In a lock server it is quite handy to use 'noreply' to
manage clients that needs to wait for the lock. 

ML> In the case of the long-running request, what I do instead is
ML>   {reply, {pending, Ref}, State}
ML> where Ref is a reference which is then included in a later message
ML> sent the old-fashioned way. This amounts to more or less the same as
ML> 'noreply', though it's one message more expensive and has the benefit
ML> of making it easier to reason about timeouts, at least to my mind.

If the reply is really critical for the client (that is
it cannot do anything useful without the reply) it
would be simplest for the client to just wait
indefinitely for the reply.

Assuming that the client can do something useful while
waiting for the reply, it could make sense to use the
{pending, Ref} strategy. But would it not even be
simpler to use an explicit receive in the client,
instead of polling the server repeatedly for the
outcome of {pending, Ref} in that case?


More information about the erlang-questions mailing list