gpt4 book ai didi

clojure - 在 clojure 中处理来自 http 服务器的消息流

转载 作者:行者123 更新时间:2023-12-02 23:24:49 26 4
gpt4 key购买 nike

我正在寻找一种惯用的方法来执行以下操作。我有一个 http 服务器,它对特定的 GET 请求响应消息流。现在,由于此消息是非终止的,因此当我使用 clj-http/get 时,调用将永远阻塞(我正在使用 LightTable)。我想设置一个回调或 core.async 样式 channel ,以便在消息传入时对其进行一些操作。甚至将流写入文件对我来说也是一个很好的第一步。有什么指点吗?调用如下:

    (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})) 和一个输出流并从一个流复制到另一个)。类似的东西

(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 处理它们,我们可能希望使用 readerline-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

你可以利用它作为一个改进的机会。特别是,这些信息足以为交易建立一个转换器,可以将交易转换为更方便使用的格式,例如 map 。此外,我们可能需要一种方法来停止获取元素并在某个时候关闭连接。我自己对 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))

关于clojure - 在 clojure 中处理来自 http 服务器的消息流,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30125909/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com