[erlang-questions] When to use gen_server?
Thu Jan 12 12:22:04 CET 2012
On Thu, Jan 12, 2012 at 10:35 AM, eigenfunction <>wrote:
> > If you don't need to serialize your data
> > processing requests, don't bother with all that.
> Could you pls elaborate more on that? I, Like the OP , sometimes
> have a hard time making a difference between a gen_server and a normal
> and i end up using normal processes and think that i am doing
> something wrong.
> Can anyone give an example of a case where a gen_server is more useful
> than a normal
A gen_server wraps up a resource through client-server interaction. From
the documentation (
"The client-server model is characterized by a central server and an
arbitrary number of clients. The client-server model is generally used for
resource management operations, where several different clients want to
share a common resource. The server is responsible for managing this
The key here is that you want multiple clients to share one common
resource. Thus you have to provide a way to handle multiple requests from
your clients in a defined way. The gen_server does that for you.
Sometimes your model does not need a shared common resource, instead you
bring lots of processes to life that do work.
For example, I use a gen_server to handle comms with a C Node that runs Lua
code. This means I don't have to worry about asynchronous requests in my C
or Lua code.
I have another component that handles messages from RabbitMQ and here I
simply care about handling the message, so I kick of a process to deal with
the message. (This is low-volume messaging, in case you start wondering
about the sanity of this approach :-)
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the erlang-questions