Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (defn wget []
- (slurp "http://xx.yy"))
- (defn show [s]
- (print s)
- (flush))
- (defn snag-futures [n]
- ;; Lazy q will be evaluated in chunks of 32 launching that many
- ;; futures at a time:
- (let [q (for [_ (range n)]
- (do
- (show "+")
- (future (let [x (wget)]
- (show ".")
- x))))]
- ;; mapv reterns a vec, not a lazy seq. Hm, launching all futures
- ;; by (vec q) does not seem to help performance:
- (mapv deref q)))
- (defn snag-quotes [n]
- (let [c (chan)]
- ;; Posting to the channel here, each from a separate "thread". An
- ;; exception that prevents a put would deadlock the reader down
- ;; there:
- (dotimes [n n]
- (go (>! c (try
- (show "+")
- (let [x (wget)]
- (show ".")
- x)
- (catch Exception e
- ;; (str (.getMessage e))
- e)))))
- ;; Reading n items from the channel here. The vec is delivered
- ;; through a go-block result channel. There is also go-loop for
- ;; this:
- (<!! (go (loop [n n
- acc []]
- (if (zero? n)
- acc
- (recur (dec n)
- (conj acc (<! c)))))))))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement