On Mon, Feb 28, 2011 at 9:34 AM, Zlaja <zlatko.jo...@gmail.com> wrote:
> Hi,
>
> How I can achieve master worker pattern in clojure with agents.
> For example I have components that write messages to a queue.
> I would like to process messages parallel by agents. Is it posible?
>
> Thanks

It may be possible to be even simpler than that and use the implicit
agent job queue.

Potentially, as simple as just using (send agt process-message
the-message), or (send-off agt process-message the-message). If
process-message needs to work with a context object (e.g. an HTTP
session or DB connection), or update a data structure, just return
this object (or an updated version of it) and have it be the agent's
state. If process-message is stateless or all its state is bound up in
I/O (e.g. database tables) with a global connection (e.g. *db*), just
keep the agent's state nil -- create it with (agent nil) and make sure
process-message always returns nil (otherwise, the agent may hang onto
random, disused objects, preventing their eligibility for GC, which
may waste substantial amounts of heap if these are sometimes large; a
lot of functions you may call for their side effects nonetheless
return a value of some kind, though others, like println, do not).

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Reply via email to