Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Synchronization of the standard output in Clojure

I have a multithreaded application written in Clojure. There is a problem of making a text in the console display correctly when multiple threads write to STDOUT. How can I do this correctly in Clojure, so the lines won't look interlaced? I think this would involve some kind of separate IO agent, but I'm not really sure how to do that.

like image 466
bvk256 Avatar asked Dec 07 '11 16:12

bvk256


1 Answers

I think this would involve some kind of separate IO agent

Yes, that should work. Create an agent (def printer (agent nil)) and call it with the appropriate print statement, e.g, (send printer #(println msg)). The messages are put in a queue and are executed (asynchronously) one at a time.

For logging purposes you could also look at tools.logging which uses agents under the hood.

like image 106
Jonas Avatar answered Nov 13 '22 18:11

Jonas