Does anyone know of any libraries for Clojure that wrap up non-blocking file I/O?
I'm interested in one suitable for use with core.async on Java 7+. Below is a naïve stab at a function that reads file and returns it on a channel. It blocks somewhat (in the open and length calls), but overall, it is trying to be fast and non-blocking enough to usable in one of the core.async worker threads under mild load. Of course, there are lots of things to be concerned about, such as whether it is safe to pass a ByteBuffer across a channel (thread safety concerns), etc. It also puts exceptions on the channel, so the client can use Nolen's <? macro [1]. I'd rather not be solving a problem alone that I believe lots of core.async users will encounter. (def asynch-file-executor (Executors/newFixedThreadPool 8)) (defn read-file [file] (let [ch (async/chan) path (java.nio.file.Paths/get (java.net.URI. file)) fileChannel (AsynchronousFileChannel/open path #{StandardOpenOption/READ} asynch-file-executor (make-array FileAttribute 0)) buffer (ByteBuffer/allocate (.length (.toFile path)))] (.read fileChannel buffer 0 buffer (proxy [CompletionHandler] [] (completed [result _] (.close fileChannel) (.flip buffer) (async/go (async/>! ch buffer) (async/close! ch))) (failed [e _] (async/go (async/>! ch e) (async/close! ch))))) ch)) [1] http://swannodette.github.io/2013/08/31/asynchronous-error-handling/ -- You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@googlegroups.com Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en --- You received this message because you are subscribed to the Google Groups "Clojure" group. To unsubscribe from this group and stop receiving emails from it, send an email to clojure+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.