[erlang-questions] Where can I use gen_server?

ryeguy <>
Thu Apr 2 04:34:44 CEST 2009


Well why doesn't this work then?

start()               -> gen_server:start({global, ?MODULE}, ?MODULE,
[], []).
stop()                -> gen_server:call({global, ?MODULE}, stop).
test()                -> gen_server:cast({global, ?MODULE}, test).
test2()                -> gen_server:cast({global, ?MODULE}, test2).

init([]) ->process_flag(trap_exit, true),
    {ok, 0}.

handle_cast(test, State) ->
	timer:sleep(3000),
	io:format("asdasd"),
	{noreply, State};
handle_cast(test2, State) ->
	timer:sleep(3000),
	3=4,                 %%% deliberate error
	{noreply, State}.

handle_call(stop, _From, State) ->
    {stop, normal, stopped, State};
handle_call(test2, _From, State) ->
	3=4,
    {noreply, State}.

handle_info(_Info, State) ->
    {noreply, State}.

terminate(_Reason, _State) ->
    ok.

code_change(_OldVsn, State, _Extra) ->
    {ok, State}.

When I call test2() then call test(), test2 (obviously) crashes, but
test() appears to be terminated too.

On Apr 1, 10:12 am, Bengt Kleberg <> wrote:
> Greetings,
>
> If you start a gen_server with start_link() it will take its clients
> with it. If you start with start() the clients will remain.
>
> bengt
>
>
>
> On Mon, 2009-03-30 at 19:29 -0700, ryeguy wrote:
> > I'm making a game server, and I just can't see where I would want to
> > use a gen_server. I'm not set on using one, but I don't want to skimp
> > on using one where it would make it easier to program this app.
>
> > What I have per node is this: I have 1 process accepting new
> > connections, which spawns a new process for each new client. For each
> > packet that comes in, a new process is spawned to handle it and then
> > it sends the result back to the listening process and terminates.
>
> > It sounds like a good candidate for gen_server, but per the docs it
> > seems you want to use them when there is some shared resource that you
> > want to manage, and I don't see one. If I gave each client their own
> > gen_server, it wouldn't be good because if an erroneous packet was
> > sent that caused an exception, it would terminate their connection and
> > any other packets being processed for this client (since a gen_server
> > takes all of its spawned processes down with it, right?).
>
> > Is there any advantage to using gen_server in my situation?
> > _______________________________________________
> > erlang-questions mailing list
> > 
> >http://www.erlang.org/mailman/listinfo/erlang-questions
>
> _______________________________________________
> erlang-questions mailing list
> ://www.erlang.org/mailman/listinfo/erlang-questions



More information about the erlang-questions mailing list