随着许多go块一下子进入,所有写入控制台,当文本到达控制台时,文本可能会混乱/混合。如何避免这种情况,因此当从go块中发出跟踪输出时,控制台中的跟踪输出是否正确?
答案 0 :(得分:2)
这个答案使用core.async
本身。以下是谈话:
;;;;; 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")
从here
逐字复制的代码Timothy Balridge的谈话是here
我有一个atom
用于打开和关闭调试。要准确显示显示的消息,用法如下:
(u/log @debug (str "Asked " info-ele ", and got back: " some-return))
在另一端像这样:
(defn log [debug msg]
(when debug (>!! log-chan msg)))
答案 1 :(得分:1)