On Sat, 17 Oct 2009, mbrodersen <morten.broder...@gmail.com> writes: > If you want to print to stdout from multiple threads without getting > the printing garbeled you can do something like the following (it also > logs all printed values): > > (def wa-debug-agent) > > (defn wa-debug-make [] > (def wa-debug-agent (agent []))) > > (defn wa-debug-print > "This makes it possible to print from multiple threads without > overlapping each other" > [& args] > (send wa-debug-agent > (fn [list v] > (apply println v) > (conj list v)) args))
Here is another variant using atoms instead. (defn serial-println [_ _ _ args] (apply println args) (flush)) ; #'user/serial-println (def serial-stream (atom nil)) ; #'user/serial-stream (add-watch serial-stream :default serial-println) ; #<a...@10ade7e: nil> (defn serial-enqueue [& args] (swap! serial-stream (fn [_] args))) ; #'user/serial-enqueue (def sprintln serial-enqueue) ; #'user/sprintln (dotimes [i 10] (.run (Thread. #(let [d (rand-int 2000)] (Thread/sleep d) (sprintln "Id:" i "Duration:" d))))) ; Id: 0 Duration: 1228 ; Id: 1 Duration: 1067 ; Id: 2 Duration: 893 ; Id: 2 Duration: 893 ; Id: 3 Duration: 1482 ; Id: 4 Duration: 1514 ; Id: 5 Duration: 79 ; Id: 6 Duration: 1966 ; Id: 7 Duration: 730 ; Id: 8 Duration: 1489 ; Id: 9 Duration: 1919 Regards. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---