gen_server partial replies

Mikael Karlsson mikael.karlsson@REDACTED
Sat Nov 22 14:59:39 CET 2003

fredag 21 november 2003 17:29 skrev Bengt Kleberg:

> why do you want to do a partial reply? if it is to free up the server
> for other call's, then you could
> 1 spawn a new process
> 2 return {noreply, State};
> 3 use gen_server:reply(Pid, Reply), from the spawned process, when ready
> to do so
> bengt

Great info, I did not know this!
I can see from the gen_server code that this works for handle_call/3 were you 
have a choice of returning {reply, Reply, NState} or {noreply, NState}.  
In the system documentation:
this is not obvious to me though, as I can only see the asynchronous 
handle_cast mentioned for {noreply, NState}.

I think this is a common pattern in many situations were you want to have a 
choice of returning at once or later. If I have a (proxy) cache I want to 
return at once at hits, but later if I have to go and ask a remote system for 

One thing in the gen_server code bothers me a bit though. There is the main 
loop function which calls the handle_msg function which calls the loop 
function and so on. Is this OK for an infinite process? Will the compiler 
handle the stack properly?

loop(Parent, Name, State, Mod, Time, Debug) ->  ...
    handle_msg(Msg, Parent, Name, State, Mod, Time);

handle_msg({'$gen_call', From, Msg}, Parent, Name, State, Mod, _Time) ->
    case catch Mod:handle_call(Msg, From, State) of
	{reply, Reply, NState} ->
	    reply(From, Reply),
	    loop(Parent, Name, NState, Mod, infinity, []);


More information about the erlang-questions mailing list