[erlang-questions] Erlang way for process-as-library?

Hakan Mattsson hakan@REDACTED
Thu Feb 8 10:06:30 CET 2007


A totally different approach is to use a supervisor.
Besides the process obvious supervision (which is
desirable for many persistent server processes), you
would also get a serialized startup procedure.

/Håkan

On Wed, 7 Feb 2007, Torbjorn Tornkvist wrote:

TT> I'm surprised over the long thread my rough sketch caused.
TT> In real life I would of course do almost as in Mats proposal,
TT> with some minor changes... :-)
TT> 
TT> Mats Cronqvist wrote:
TT> > 
TT> > start() ->
TT> >    spawn(fun init/0) ! {hello,self()},
TT> >    receive {ok,Pid} -> Pid
TT> >    end.
TT> 
TT> start() ->
TT>   Self = self(),
TT>   Pid = spawn(fun() -> init(Self) end),
TT>   receive {ok, Pid, RealPid} -> RealPid end.
TT> 
TT> > 
TT> > init() ->
TT> >    receive {hello,Daddy} -> ok end,
TT> >    try register(name,self()), Daddy ! {ok,self()}
TT> >    catch error:badarg -> Daddy ! {ok,whereis(name)},exit(normal)
TT> >    end,
TT> >    loop().
TT> 
TT> init(Daddy) ->
TT>   try register(name,self()), Daddy ! {ok,self(),self()}
TT>   catch error:badarg -> Daddy ! {ok,self(),whereis(name)},exit(normal)
TT>   end,
TT>   loop().
TT> 
TT> 
TT> --Tobbe


More information about the erlang-questions mailing list