dan*_*dan 21 concurrency clojure pmap
关于该pmap函数的文档让我想知道如何通过Web获取XML提要集合的效率.我不知道pmap会产生多少并发获取操作以及最大值.
Ale*_*ler 21
如果您检查来源,您会看到:
> (use 'clojure.repl)
> (source pmap)
(defn pmap
"Like map, except f is applied in parallel. Semi-lazy in that the
parallel computation stays ahead of the consumption, but doesn't
realize the entire result unless required. Only useful for
computationally intensive functions where the time of f dominates
the coordination overhead."
{:added "1.0"}
([f coll]
(let [n (+ 2 (.. Runtime getRuntime availableProcessors))
rets (map #(future (f %)) coll)
step (fn step [[x & xs :as vs] fs]
(lazy-seq
(if-let [s (seq fs)]
(cons (deref x) (step xs (rest s)))
(map deref vs))))]
(step rets (drop n rets))))
([f coll & colls]
(let [step (fn step [cs]
(lazy-seq
(let [ss (map seq cs)]
(when (every? identity ss)
(cons (map first ss) (step (map rest ss)))))))]
(pmap #(apply f %) (step (cons coll colls))))))
Run Code Online (Sandbox Code Playgroud)
这(+ 2 (.. Runtime getRuntime availableProcessors))是一个很大的线索.pmap将抓取第一(+ 2 processors)部分工作并通过异步运行它们future.因此,如果你有2个核心,它将一次启动4个工作,试图保持领先于你,但最大应该是2 + n.
future最终使用代理I/O线程池,它支持无限数量的线程.它会随着工作的增加而增长,如果线程未被使用则会缩小.
mik*_*era 11
基于Alex的优秀答案,解释了pmap如何工作,这是我对你的情况的建议:
(doall
(map
#(future (my-web-fetch-function %))
list-of-xml-feeds-to-fetch))
Run Code Online (Sandbox Code Playgroud)
理由:
| 归档时间: |
|
| 查看次数: |
5242 次 |
| 最近记录: |