[erlang-questions] When to use gen_server?
Robert Raschke
rtrlists@REDACTED
Thu Jan 12 12:22:04 CET 2012
On Thu, Jan 12, 2012 at 10:35 AM, eigenfunction <emeka_1978@REDACTED>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
> process,
> 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
> process?
>
>
A gen_server wraps up a resource through client-server interaction. From
the documentation (
http://www.erlang.org/doc/design_principles/gen_server_concepts.html#id55988
):
"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
resource."
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 :-)
Robby
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20120112/fca16317/attachment.htm>
More information about the erlang-questions
mailing list