How about using agents instead of locks to ensure this?
I think that using agents to safely save the general changed state, regardless of whether it is in memory or on disk, is more idiomatic in clojure than using locks.
If you create one agent at a time and send attempts to access the agents, you can make sure that only by stream during access to this file.
For example, for example:
(use 'clojure.contrib.duck-streams) (defn file-agent [file-name] (add-watch (agent nil) :file-writer (fn [key agent old new] (append-spit file-name new)))) (defn async-append [file-agent content] (send file-agent (constantly content)))
then add the file through the agent:
(async-append "content written to file" (file-agent "temp-file-name"))
If you require synchronous use of the file, this can be achieved with anticipation. Like this:
(defn sync-append [file-agent content] (await (send file-agent (constantly content))))
Verneri ร
berg
source share