【发布时间】:2013-07-10 05:33:49
【问题描述】:
我正在研究在 Clojure 中发出并行 http 请求的示例,
http://lethain.com/a-couple-of-clojure-agent-examples/
特别是
(ns parallel-fetch
(:import [java.io InputStream InputStreamReader BufferedReader]
[java.net URL HttpURLConnection]))
(defn get-url [url]
(let [conn (.openConnection (URL. url))]
(.setRequestMethod conn "GET")
(.connect conn)
(with-open [stream (BufferedReader.
(InputStreamReader. (.getInputStream conn)))]
(.toString (reduce #(.append %1 %2)
(StringBuffer.) (line-seq stream))))))
(defn get-urls [urls]
(let [agents (doall (map #(agent %) urls))]
(doseq [agent agents] (send-off agent get-url))
(apply await-for 5000 agents)
(doall (map #(deref %) agents))))
(prn (get-urls '("http://lethain.com" "http://willarson.com")))
当我在
IllegalStateException await-for in transaction
这是什么意思,我该如何解决?
【问题讨论】:
-
奇怪...我刚刚尝试了代码,但没有收到
IllegalStateException异常。错误消息来自await-for的定义,但它只应在事务内部使用该函数时显示(即在dosync内部),因为这就是io!宏的用途,而我不是在您的代码中查看单个事务。 -
您能否再次检查您是否在
get-urls中不小心将doseq输入为dosync,同时移动doseq表单的最后一个结束括号以包含@987654333 @ 在它的身体里? -
是的,仔细检查。当我将代码放入 .clj 文件中然后尝试从 repl 加载它时出现此错误:
(use 'parallel-fetch :reload-all) IllegalStateException await-for in transaction clojure.core/await-for (core.clj:2942) -
如果注释掉
(prn (get-urls '("http://lethain.com" "http://willarson.com"))),通过(use 'parallel-fetch :reload-all)加载代码,然后从REPL执行(prn ,,,)呢?我的猜测是use正在将代码加载到执行await-for的事务中。
标签: clojure urlconnection agent illegalstateexception