Get the output of a long process

There is a lengthy Unix process that I would like to write and process using Clojure. A good example of one of these processes is the repl-y / nREPL session: its duration is indefinite, and the output is printed to standard output.

If I try (clojure.java.io/sh "lein" "repl") , the evaluation will be blocked until the main process is complete, and then I can observe the result.

This is not what I want - I want to receive the stream immediately.

Can I achieve this with Clojure.java.io or similar existing Clojure tools? Do not mind resorting to Java otherwise.

+4
source share
2 answers

Take a look at the me.raynes.conch library, which is slightly more versatile than clojure.java.shell. This low-level API seems to be what you are looking for.

+3
source

Not a detailed answer, but the source for the Clojure sh function is pretty short. If you redid it a bit to remove .waitFor (or added a higher order function to consume the partial readings returned by InputStreamReader as they arrive), you could probably get updated data as they are returned by the process. But be careful with deadlocks if your subprocess also expects input (as in your lein repl example).

+1
source

All Articles