[erlang-questions] multiple instances of gen_server

Jani Launonen launoja@REDACTED
Fri Oct 26 00:20:31 CEST 2007



----- Alkuperäinen viesti -----
Lähettäjä: YC <yinso.chen@REDACTED>
Päiväys: perjantai, lokakuu 26, 2007 1:10 am
Aihe: [erlang-questions] multiple instances of gen_server
Vastaanottaja: erlang-questions Questions <erlang-questions@REDACTED>

> Hi all -
> 
> can there be multiple instances of a gen_server module on the 
> same node?  I
> couldn't find the answer googling (though I probably missed it), 
> but reading
> through the source code it seems that the server process is 
> registered by
> name, so by default it appears to be a singleton.
> 
> The gen_server start template from erlang mode and programming 
> erlang looks
> like
> 
> gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).
> 
> Could it be that to enable multiple instances, instead of using 
> ?MODULE as
> the name, I pass in a different name myself and explicitly pass 
> the name
> through the API to get to the correct server?

The documentation says that (http://www.erlang.org/doc/design_principles/gen_server.html#2.3):

"start_link() ->
    gen_server:start_link({local, ch3}, ch3, [], []) => {ok, Pid}
    
start_link calls the function gen_server:start_link/4. This function spawns and links to a new process, a 
gen_server.


The first argument {local, ch3} specifies the name. In this case, the gen_server will be locally registered as 
ch3.
If the name is omitted, the gen_server is not registered. Instead its pid must be used. The name could also 
be given as {global, Name}, in which case the gen_server is registered using global:register_name/2."

So, yes. You could create unique names for each gen_server, or not to bother to name them at all. You could 
use only the PID to communicate with the gen_server instance. 
 
> Any insights are appreciated, thanks,
> yc

Cheers,
Jani L.



More information about the erlang-questions mailing list