Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/clojure/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asynchronous 如何确保所有数据都从clojure';写入文件;s核心异步通道?_Asynchronous_Clojure_Core.async - Fatal编程技术网

Asynchronous 如何确保所有数据都从clojure';写入文件;s核心异步通道?

Asynchronous 如何确保所有数据都从clojure';写入文件;s核心异步通道?,asynchronous,clojure,core.async,Asynchronous,Clojure,Core.async,我对core.async非常陌生,我一直在尝试了解如何最好地将core.async与文件IO结合使用。我做了一个测试,虽然打印到控制台工作正常,但无法写入最后一个文件。知道我错过了什么吗 首先,一些功能 (defn thread-write-to-files [channel] (let [writer (atom nil)] (thread (loop [] (when-some [value (<!! channel)] (i

我对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)))))))

测试的最后一个条件失败。文件baz.txt已创建,但为空。我的REPL打印出输入中的每一行,因此我不明白为什么文件仍然是空的。

线程写入文件中
当输入通道关闭时(当
时)(当某个[value(获取零并退出
循环时)
您的测试从未关闭通道,因此不会发生这种情况。请尝试在
输出通道上使用
close!
,或者使用而不是
来更改
,将测试数据采集放入系统中

(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))))