[英]Processing a stream of messages from a http server in clojure
我正在寻找一种惯用的方法来做到以下几点。 我有一个http服务器,在特定的GET请求上响应消息流。 现在,由于此消息是非终止的,当我使用clj-http / get时,调用会永远阻塞(我正在使用LightTable)。 我想建立一个回调或core.async样式的通道来对消息进行一些操作。即使将流写入文件对我来说也是一个很好的第一步。 有什么指针吗? 这是电话:
(require '[clj-http.client :as client])
(def url "http://hopey.netfonds.no/tradedump.php?date=20150508&paper=AAPL.O&csv_format=txt")
(client/get url)
日期必须更改为今天的数据流日期。 谢谢!
要将流写入文件,一个简单的方法是使用clojure.java.io/copy
(它接受输入流,例如(:body (client/get some-url {:as :stream}))
返回的输入流(:body (client/get some-url {:as :stream}))
和输出流以及从一个到另一个的副本)。 就像是
(ns http-stream
(:require [clj-http.client :as client]
[clojure.java.io :as io]))
(with-open [in-stream (:body (client/get "http://hopey.netfonds.no/tradedump.php?date=20150508&paper=AAPL.O&csv_format=txt" {:as :stream}))
out-stream (->> "streamoutput.txt"
io/as-file
io/output-stream)]
(io/copy in-stream out-stream))
这让我在几秒钟内获得了数千行标签分隔值。 现在,要在行级别使用core.async处理它们,我们可能希望使用reader
和line-seq
更多地处理流:
(ns http-stream
(:require [clj-http.client :as client]
[clojure.core.async :as async]
[clojure.java.io :as io]
[clojure.string :as str]))
(defn trades-chan
"Open the URL as a stream of trades information. Return a channel of the trades, represented as strings."
[dump-url]
(let[lines (-> dump-url
(client/get {:as :stream})
:body
io/reader
line-seq) ];;A lazy seq of each line in the stream.
(async/to-chan lines))) ;;Return a channel which outputs the lines
;;Example: Print the first 250 lines.
(let [a (trades-chan "http://hopey.netfonds.no/tradedump.php?date=20150508&paper=AAPL.O&csv_format=txt")]
(async/go-loop [takes 250]
(when (< 0 takes)
(println (async/<! a))
(recur (dec takes)))))
现在,有了这个,你很大程度上已经启动了,但是我注意到流总是以对列的描述开始
time price quantity board source buyer seller initiator
你可以用它作为改善一点点的机会。 特别是,这足以为交易建立一个传感器的信息,可以将交易变成更方便的格式,如地图。 此外,我们可能想要一种方法来停止使用元素并在某个时候关闭连接。 我自己并不熟悉core.async,但这似乎有效:
(defn trades-chan
"Open the URL as a tab-separated values stream of trades.
Returns a core.async channel of the trades, represented as maps.
Closes the HTTP stream on channel close!"
[dump-url]
(let[stream (-> dump-url
(client/get {:as :stream})
:body)
lines (-> stream
io/reader
line-seq) ;;A lazy seq of each line in the stream.
fields (map keyword (str/split (first lines) #"\t")) ;; (:time :price :quantity ...
transducer (map (comp #(zipmap fields %) #(str/split % #"\t"))) ;;A transducer that splits strings on tab and makes them into maps with keys from fields
output-chan (async/chan 50 transducer)]
(async/go-loop [my-lines (drop 1 lines)]
(if (async/>! output-chan (first my-lines)) ;;If we managed to put
(recur (rest my-lines)) ;;then the chan is not closed. Recur with the rest of the lines.
(.close stream))) ;;else close the HTTP stream.
output-chan))
我认为user1571406的答案是合理的,并且给出了将clj-http
与core.async
结合起来的一个很好的介绍。 但是,如果您不坚持使用clj-http
,我强烈推荐使用http-kit库,它更适用于异步响应处理。 使用http-kit
,您可以按如下方式编写回叫。
user> (require '[clojure.java.io :as io]
'[org.httpkit.client :as h])
nil
user> (def url "http://hopey.netfonds.no/tradedump.php?date=20150508&paper=AAPL.O&csv_format=txt")
#'user/url
user> (h/get url {:as :stream}
(fn [{:keys [status body]}]
(if (= status 200)
(with-open [out (io/output-stream "/tmp/output.txt")]
(io/copy body out)))))
#<core$promise$reify__6363@373b22df: :pending>
最后一个h/get
函数调用立即返回,其回调函数fn
异步将响应体写入文件/tmp/output.txt
。
(ns asyncfun.core
(:require [clojure.core.async :as async
:refer [<! >!! go chan]]
[clj-http.client :as client]))
(def url "http://hopey.netfonds.no/tradedump.php?date=20150508&paper=AAPL.O&csv_format=txt")
(def out-chan (chan))
(go (println (<! out-chan)))
(>!! out-chan (client/get url))
我把这段代码放在一起几分钟。 我认为core.async就是你要找的东西。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.