On Mon, May 10, 2010 at 10:18 PM, Tim Morgan <seve...@gmail.com> wrote:
> OK, I incorporated some of the suggestions mentioned here, including > the idea of using futures instead of agents. > > Unfortunately, my script is now 3-4 times slower when using futures: > http://gist.github.com/395153#file_pingdom_with_futures.clj > > I have a feeling it has something to do with the work being network I/ > O bound rather than CPU bound -- something I noticed when using > agents. The docs say when it's network I/O bound, to use send-off > rather than just send when working with agents. But I see nothing > about the type of work when dealing with futures. I'm sure I'm missing > something. > Futures use the same threadpool as agents actions sent by send-off. What bites you is lazy-evaluation: (defn ping-ips [subnet] (let [subnet-with-dot (replace-first-re #"\.?$" "." subnet) ips (map #(str subnet-with-dot %) (range 1 255)) futures (map #(future {:ip %, :exists (host-exists? %)}) ips)] (map deref futures))) Here, the production of the resulting seq blocks until the 1st future is done, then it creates the second future and waits for it etc. You have to force realization of futures (eg with dorun or by turning futures into a vector): (defn ping-ips [subnet] (let [subnet-with-dot (replace-first-re #"\.?$" "." subnet) ips (map #(str subnet-with-dot %) (range 1 255)) futures (map #(future {:ip %, :exists (host-exists? %)}) ips)] (dorun futures (map deref futures))) Christophe -- Brussels, 23-25/6 http://conj-labs.eu/ Professional: http://cgrand.net/ (fr) On Clojure: http://clj-me.cgrand.net/ (en) -- 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