Skip to content

Instantly share code, notes, and snippets.

@angerman angerman/agents.clj
Created Dec 1, 2009

Embed
What would you like to do?
;; Worker agents are there to be dipatched to, they state should be incremented
;; on each completed work unit
(def *worker* (map #(agent -1 :meta {:no %}) (range 5)))
;; a simualted work unit for the worker
(defn work-unit [s n]
(Thread/sleep 1000)
;;(send printer println+ (format "%s: %d" (str *agent*) n))
(inc s))
;; the printer agent coordinates the printing to the console
;; alternativly using a logfile
(def printer (agent nil :meta {:out *out*}))
;; the printer's println funktion
(defn println+ [_ msg] (binding [*out* (:out ^*agent*)] (println msg)))
;; The tasks agent, holds all the current tasks to be executed
(def taskmaster (agent (range 60)))
;; Takes an additional agent, and dispatches the first task from the queue (agents state) to the
;; specified agent
(defn next+ [tasks agent]
(if-let [task (first tasks)]
(do
(send printer println+ (format "Dispatched Task %d to Agent No.: %d (completed: %d)" task (:no ^agent) @agent))
(send agent work-unit task)
(drop 1 tasks))))
;; hookup the taskmaster as a watcher over his workers
(doseq [worker *worker*]
(add-watcher worker :send taskmaster next+))
;; remove the taskmaster
(doseq [worker *worker*]
(remove-watcher worker taskmaster))
;; start the agents:
(doseq [worker *worker*]
(send worker inc))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.