Clojure:当在go块或线程中执行代码时,记录到控制台 [英] Clojure: log to console when code is being executed in a go block or thread
问题描述
有许多go块一次,所有写入控制台,文本可以得到混乱/混合,当它到达控制台。如何避免这种情况,因此,当在go块内发出时,跟踪输出在控制台中正确显示?
With many go blocks going at once, all writing to the console, the text can get jumbled/intermingled when it arrives at the console. How to avoid this, so trace output forms as correctly in the console as intended when it was emitted from within the go block?
推荐答案
这个答案使用 core.async
本身。下面的讲座:
This answer uses core.async
itself. The following from a talk:
;;;;; Logging Handler ;;;;;
(def log-chan (chan))
(thread
(loop []
(when-let [v (<!! log-chan)]
(println v)
(recur)))
(println "Log Closed"))
(close! log-chan)
(defn log [msg]
(>!! log-chan msg))
(log "foo")
从这里
Talk是由Timothy Balridge和是这里
Talk was by Timothy Balridge and is here
我有 atom
用于打开和关闭调试。对于显示的消息,使用方式如下:
I have an atom
for turning debugging on and off. To be precise about the messages that are displayed the usage is like this:
(u/log @debug (str "Asked " info-ele ", and got back: " some-return))
:
At the other end like this:
(defn log [debug msg]
(when debug (>!! log-chan msg)))
这篇关于Clojure:当在go块或线程中执行代码时,记录到控制台的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!