1
我通過Clojure中進行並行http請求的例子工作,Clojure的代理中並行HTTP IllegalStateException異常並等待換
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']的定義(http://clojuredocs.org/clojure_core/clojure.core/await-for),但它只有在事務內部使用該函數時纔會顯示即在'dosync'中),因爲這就是'io!'宏的用途,而且我在代碼中看不到單個事務。 –
您是否可以在移動'doseq'表單的最後一個關閉表達式以包含'(apply await-for)'時,檢查您是否沒有在'get-urls'中意外鍵入'doseq'作爲'dosync'。 ..)'在它的身體? –
是的,雙重檢查。當我將代碼放入.clj文件,然後嘗試從repl中加載代碼時,出現此錯誤: IllegalStateException await-for在事務clojure.core中/ await-for(core.clj:2942)' –