Sorta off-topic from the main discussion, but in reference to the error you 
pointed out, one clever fix for this is to add a delay around the future:

(defn cache-image [icache url]
  (let [task (delay (future (download-image url)))]
    (doto (swap! icache update-in [url] #(or % task))
      (-> (get url) (force)))))

(defn wait-for-image [icache url]
  (deref (force (get icache url))))

As long as your swap! never overwrites an existing delay, only one will get 
added, and thus only one gets forced.

On Wednesday, July 18, 2012 4:48:17 AM UTC-7, tbc++ wrote:
>
> (def icache (atom {})) 
>
> (defn cache-image [url] 
>    (let [f (future (download-image url))] 
>      (swap! icache assoc url f))) 
>
> I'll admit, we'll still try to download the image twice if two people 
> try to cache the same image at the exact same time but we could get 
> around that using promise or agents. So in general, your attitude when 
> using atoms, refs, or agents should be "get in and get out". Don't do 
> your work inside of swap!, send or alter.... 
>

-- 
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