我有两个文件,两个文件都大1个字节(仅包含“1”或“0”)。

现在,在用Clojure编写的程序的主循环中,我想等到两个文件中的任何一个更改后再继续。这可以通过繁忙等待来完成,使用slurp轮询文件以查找更改。但这是浪费资源。

没有忙碌的等待怎么办?

最佳答案

在Java VM> 7上,我会说您可以使用Watch Service API。您可以直接使用它,也可以使用Clojure的现有包装器之一,因为其中有一个abundance

  • https://github.com/juxt/dirwatch
  • https://github.com/rplevy/ojo
  • https://github.com/derekchiang/Clojure-Watch

  • 使用Clojure-Watch,它的外观如下:
    (ns clojure-watch.example
     (:require [clojure-watch.core :refer [start-watch]]))
    
    (start-watch [{:path "/home/niko/project/hello"
               :event-types [:create :modify :delete]
               :bootstrap (fn [path] (println "Starting to watch " path))
               :callback (fn [event filename] (println event filename))
               :options {:recursive true}}])
    

    ```

    您可以收听文件修改,删除和创建。 Watch API会在可能的情况下使用本机文件系统支持来进行文件更改通知。

    关于events - Clojure-等待文件更改,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34506654/

    10-12 07:33
    查看更多