Hi,

I've not yet seen any examples on how to deal with external processes
in Clojure (I hope I didn't overlook something in clojure-contrib).

The following is my attempt to start a sub-process and to pass through
stdout and stderr.  The shell command prints out 1000 lines "hello"
and a final "command finished". The problem is that nothing is printed
by the Clojure program. If I increase the number of lines for example
to 2000 (change "head -1000" to "head -2000"), I see a lot of output,
but it is cut off somewhere in the middle and the final "command
finished" does never appear.

(use 'clojure.contrib.duck-streams)

(defn copy
    [istream ostream]
    (println "copy" istream ostream)
    (loop [line (.readLine istream)]
        (if line
            (do
                (.println ostream line)
                (recur (.readLine istream))))))

(let [pb (new ProcessBuilder ["sh" "-c" "yes hello | head -1000; echo
command finished"])
        proc (.start pb)
        stdout (reader (.getInputStream proc))
        stderr (reader (.getErrorStream proc))
        stdout-agent (agent stdout)
        stderr-agent (agent stderr)]
    (send stdout-agent copy (writer *out*))
    (send stderr-agent copy (writer *err*))
    (await stdout-agent stderr-agent)
    (.waitFor proc)
    (shutdown-agents)
    (println "done"))

Is this use of agents incorrect?
Why can the program terminate before all the output from the sub-
process has been passed through?
Is there a better way to synchronize with sub-processes in Clojure, or
is it necessary to synchronize completely at the Java level?

Thanks
Stephan
--~--~---------~--~----~------------~-------~--~----~
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
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to