(defn get-task [tasks]Ā (dosyncĀ Ā (let [task (first @tasks)]Ā Ā Ā (alter tasks rest)Ā Ā Ā task)))
(defn worker [& {:keys [tasks]}]Ā (agent {:tasks tasks}))
(defn worker-loop [{:keys [tasks] :as state}]Ā (loop [last-task nil]Ā Ā (if-let [task (get-task tasks)]Ā Ā Ā (recur task)Ā Ā Ā (locking :out (println "Last task: " last-task))))Ā state)
(defn create-workers [count & options]Ā (->> (range 0 count)Ā Ā Ā Ā (map (fn [_] (apply worker options)))Ā Ā Ā Ā (into [])))
(defn start-workers [workers]Ā (doseq [worker workers] (send-off worker worker-loop)))
(def tasks (ref (range 1 10000000)))
(def workers (create-workers 100 :tasks tasks))
(start-workers workers)(apply await workers)
(defn get-task [tasks]Ā (locking :lockĀ Ā (dosyncĀ Ā (let [task (first @tasks)]Ā Ā Ā (alter tasks rest)Ā Ā Ā task))))
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clo...@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+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to the Google Groups "Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email to clojure+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
(def tasks (ref (into [] (range 1 10000000))))
(defn get-taskĀ [tasks]Ā (dosyncĀ Ā (let [task (first @tasks)]Ā Ā Ā (alter tasks rest)Ā Ā Ā task)))
(defn worker-loopĀ []
Ā (loop [last-task nil]Ā Ā (if-let [task (get-task tasks)]Ā Ā Ā (recur task)
Ā Ā Ā last-task)))
(defn create-workersĀ [n & options]Ā (vec (repeatedly n (fn []Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā (futureĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā (worker-loop))))))
(def workersĀ (create-workers 100))
(apply max (map deref workers))
(def tasks (ref (into [] (range 1 10000000))))
(def tasks (ref (range 1 10000000)))
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to the Google Groups "Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email to clojure+u...@googlegroups.com.
Thank you! I know about Java's thread pools, but I want to do my job in clojure-idiomatic way and possibly less using java interop.