bvk256
bvk256

Reputation: 1903

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.

Upvotes: 8

Views: 765

Answers (1)

Jonas
Jonas

Reputation: 19642

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.

Upvotes: 9

Related Questions