2

I have two files, both 1 byte big (containing nothing more than "1" or "0").

Now inside of the main loop of a program written in Clojure, I would want to wait until either of the files have changed before proceeding. This could be done by busy-waiting, polling the files for changes using slurp. But this is a waste of resources.

How could I do this without busy-waiting?

2 Answers2

7

On the Java VM>7, I would say you could use the Watch Service API. You can use it directly or use of one of the already existing wrapper for Clojure, since there is quite an abundance of them.

With Clojure-Watch this is how it would look like:

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

```

You can listen to file modifications, deletion and creation. The Watch API uses the native file system support for file change notification when it can.

Nicolas Modrzyk
  • 13,961
  • 2
  • 36
  • 40
0

To complete the answer of Nicolas, there is also https://github.com/wkf/hawk which seems to work more efficiently when on MacOS.

Vincent Cantin
  • 16,192
  • 2
  • 35
  • 57