gen_server

Sean Hinde sean.hinde@REDACTED
Tue Dec 16 00:10:15 CET 2003


On 15 Dec 2003, at 22:17, Inswitch Solutions - Erlang Evaluation wrote:

>
> When calling gen_server:call, does it blocks the gen_server process?
>
> From another point of view does gen_server solve the pattern of 
> multiple
> clients requests calling in a synchronous way (gen_server:call)?
> For each client a process should be spawned.

I think what you are looking for is the gen_server:reply/2 function.

The normal pattern of usage is to spawn a new worker process in your 
handle_call/2 and instead of returning  {reply, Reply, New_state} you 
return {noreply, State}. This leaves the gen_server unblocked ready for 
new call requests, and the calling process blocked awaiting the result 
of its call

Once the worker process has completed its work it can call 
gen_server:reply(To, Reply) where To is the original caller reference 
received in the handle_call/2.

Alternatively, and what I normally do, is to store the From ref and the 
Pid of the spawned process in an ets table owned by the main gen_server 
process. The worker process then casts the answer back via the main 
gen_server process which does the gen_server:reply/2. This way if the 
worker process crashes the main gen_server can trap the 'EXIT' message, 
lookup the Pid/From, and  send an error response back to the original 
caller.

I think perhaps gen_server:reply needs to be added to the FAQ, or maybe 
a chapter devoted to this model somewhere in the docs - I have had to 
explain it many times and it is far too useful to not have prominence.

Sean




More information about the erlang-questions mailing list