You could:

* Create all futures *without* deref'ing them, so they all start in
parallel;
* Loop through the futures, asking them if they have finished, and print
those that have (and remove them from the list)

But if you want to get each result as it comes back, it's probably a better
fit for core.async than plain futures. I would suggest creating a channel
with multiple producers and a single consumer, where each producer gets a
site and the consumer prints the result. That way, you get the results as
they come.

Here is a rough draft:

(ns cjr-http-test.core
  (:require [clj-http.client :as client]
            [clojure.core.async :as async :refer [<!! >!!]]))
(defn get-heads
  [sites]
  (mapv (fn [site] (future {:head (client/head site)
                            :url site}))
        sites))
(def sites ["http://www.google.com";
            "http://www.yahoo.com";
            "http://www.bing.com";])
(defn use-futures
  []
  (let [head-requests (get-heads sites)]
    (loop [to-check head-requests checked []]
      (cond (and (empty? checked) (empty? to-check))
            :finished

            (empty? to-check)
            (recur checked [])

            (realized? (first to-check))
            (do (-> to-check first deref :url println)
                (recur (rest to-check) checked))

            :else (recur (rest to-check) (cons (first to-check) checked))))))
(defn use-async
  []
  (let [ch (async/chan)
        producers (mapv (fn [site]
                          (doto (Thread. #(>!! ch {:head (client/head site)
                                                   :url site}))
                            (.start)))
                        sites)
        close-chan (doto (Thread. (fn []
                                    (mapv #(.join %) producers)
                                    (async/close! ch)))
                     (.start))]
    (loop [v (<!! ch)]
      (if (nil? v) :finished
        (do (-> v :url println)
            (recur (<!! ch)))))))

-- 
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
--- 
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+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to