Clojure 的 pmap 函数为 URL 获取操作生成多少个线程?
关于 pmap 函数的文档让我想知道它对于通过网络获取 XML 提要集合之类的事情会有多高效。我不知道 pmap 会产生多少并发获取操作以及最大值是多少。
The documentation on the pmap
function leaves me wondering how efficient it would be for something like fetching a collection of XML feeds over the web. I have no idea how many concurrent fetch operations pmap would spawn and what the maximum would be.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您检查源代码,您会看到:
(+ 2 (.. Runtime getRuntime availableProcessors))
是一个重要线索。 pmap 将获取第一个(+ 2 个处理器)
工作,并通过future
异步运行它们。因此,如果你有 2 个核心,它将一次启动 4 个工作,试图保持领先,但最大值应该是 2+n。future
最终使用代理 I/O 线程池,它支持无限数量的线程。它会随着工作量的增加而增长,如果线程未使用,它会收缩。If you check the source you see:
The
(+ 2 (.. Runtime getRuntime availableProcessors))
is a big clue there. pmap will grab the first(+ 2 processors)
pieces of work and run them asynchronously viafuture
. So if you have 2 cores, it's going to launch 4 pieces of work at a time, trying to keep a bit ahead of you but the max should be 2+n.future
ultimately uses the agent I/O thread pool which supports an unbounded number of threads. It will grow as work is thrown at it and shrink if threads are unused.以 Alex 解释 pmap 如何工作的出色答案为基础,以下是我对您的情况的建议:
理由:
Building on Alex's excellent answer that explains how pmap works, here's my suggestion for your situation:
Rationale:
没有时间写很长的响应,但是有一个 clojure.contrib http-agent,它将每个 get/post 请求创建为自己的代理。因此,您可以发出一千个请求,它们都会并行运行并在结果出现时完成。
No time to write a long response, but there's a clojure.contrib http-agent which creates each get/post request as its own agent. So you can fire off a thousand requests and they'll all run in parallel and complete as the results come in.
看看pmap的操作,无论你有多少个处理器,它似乎一次都会运行32个线程,问题是map将领先计算32个,并且futures会自己启动。 (样本)
<代码>(defn 样本 [n]
(println“开始”n)
(线程/睡眠 10000)
n)
(def 结果 (pmap Samplef (范围 0 100)))
;您将等待 10 秒并看到 32 张照片,然后当您拍摄第 33 张照片时,再拍摄另外 32 张照片
;打印出您一次执行 32 个并发线程的分钟数
;对我来说这并不完美
;萨卢多斯·费利佩
Looking the operation of pmap, it seems to go 32 threads at a time no mater what number of processors you have, the issue is that map will go ahead of the computation by 32 and the futures are started in their own. (SAMPLE)
(defn samplef [n]
(println "starting " n)
(Thread/sleep 10000)
n)
(def result (pmap samplef (range 0 100)))
; you will wait for 10 seconds and see 32 prints then when you take the 33rd an other 32
; prints this mins that you are doing 32 concurrent threads at a time
; to me this is not perfect
; SALUDOS Felipe