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

Torbjorn Tornkvist tobbe@REDACTED
Mon Feb 5 23:43:01 CET 2007


Either let the users of the library start it with
a start function or you could let every function
do it automatically. Example:

start() ->
  case whereis(my_service) of
    Pid when pid(Pid) -> Pid
    _ -> spawn(my_mod, my_server, [])
  end.

lib_fun_1(A1, A2,...) ->
  start(),
  gen_server:call(....).

lib_fun_2(A1, A2,....) ->
  start(),
  gen_server:call(...).

I would probably prefer to only have the start function and
let the users of the library call it. Such as in e.g:

 crypto:start()


Cheers, Tobbe

Robert Baruch wrote:
> Hi all,
> 
> So I'm fooling around with writing a utility library, but to  
> initialize the library takes a little bit of time, needing to read a  
> few large files into memory. So my idea was to stick the  
> functionality into a process, so that the library could be  
> initialized once and then used by the application.
> 
> I've added the gen_server behavior to the process. So now my question  
> is, what's the Erlang way to start the process? Should I start it  
> supervised, with a supervisor running somewhere explicitly, and  
> require the user to know that they have to start up a supervisor?  
> Should I start it unsupervised, checking in every call to the library  
> whether the library is running, and if not, start it?
> 
> Help, please :)
> 
> Thanks,
> 
> --Rob




More information about the erlang-questions mailing list