gpt4 book ai didi

asynchronous - 如何确保所有数据都从 clojure core.async channel 写入文件?

转载 作者:行者123 更新时间:2023-12-02 03:18:40 25 4
gpt4 key购买 nike

我对 core.async 很陌生,我一直在尝试了解如何最好地使用 core.async 和文件 IO。我整理了一个测试,尽管打印到控制台有效,但无法写入最后一个文件。知道我缺少什么吗?

首先,一些功能...

(defn thread-write-to-files [channel]
(let [writer (atom nil)]
(thread
(loop []
(when-some [value (<!! channel)]
(if (and (map? value) (= :FILE (:type value)))
(do (when @writer (.close ^Writer @writer))
(reset! writer (io/writer (File. ^String (:name value))))
(recur))
(do (when @writer (.write @writer value)
(println value))
(recur)))))
(when @writer
(do (.flush @writer)
(.close ^Writer @writer))))))

(defn add-line-number [channel-in channel-out]
(go-loop [line-number 1]
(when-some [value (<! channel-in)]
(if (and (map? value) (= :FILE (:type value)))
(do (>! channel-out value)
(recur 1))
(do (>! channel-out (str line-number ". " value))
(recur (inc line-number)))))))

现在一个使用它们的测试......
(deftest test-thread-write-to-file
(let [input-coll ["This gets skipped"
{:type :FILE :name "foo.txt"}
"This is the first line of foo!\n"
"This is the second line of foo.\n"
{:type :FILE :name "bar.txt"}
"Bar me 1.\n"
"Bar me 2.\n"
"Bar me 3.\n"
{:type :FILE :name "baz.txt"}
"BBBBBBBBBBB\n"
"AAAAAAAAAAA\n"
"ZZZZZZZZZZZ\n"]
input-channel (async/to-chan input-coll)
output-channel (chan)
foo (File. "foo.txt")
bar (File. "bar.txt")
baz (File. "baz.txt")]
(when (.exists foo) (.delete foo))
(when (.exists bar) (.delete bar))
(when (.exists baz) (.delete baz))
(add-line-number input-channel output-channel)
(thread-write-to-files output-channel)
(Thread/sleep 1000)
(is (.exists foo))
(is (.exists bar))
(is (.exists baz))
(is (> (.length foo) 0))
(is (> (.length bar) 0))
(is (> (.length baz) 0))))

测试的最后一个条件失败。文件 baz.txt 已创建,但为空。我的 REPL 从输入中打印出每一行,所以我很困惑为什么文件仍然是空的。

最佳答案

thread-write-to-files当输入 channel 关闭时(当 (when-some [value (<!! channel)] ...) 为 nil 并退出 loop 时),您执行最终文件的刷新和关闭。
您的测试永远不会关闭 channel ,因此不会发生这种情况。尝试使用 close!output-channel , 或者使用 onto-chan 而不是 to-chan将测试数据收集到系统中。

关于asynchronous - 如何确保所有数据都从 clojure core.async channel 写入文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34868946/

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