我有这个Clojure代码,它启动和执行一个函数。
(import [java.lang Thread])
(defn with-new-thread [f]
(.start (Thread. f)))
(with-new-thread (fn [] (print "hi")))但是,当我在emacs中运行它时,它是以粘液-repl模式(用cider-jack-in执行)运行的,没有打印出来,但是没有返回。

使用lein real,我得到了预期的输出。
user=> (import [java.lang Thread])
java.lang.Thread
user=> (defn with-new-thread [f] (.start (Thread. f)))
#'user/with-new-thread
user=> (with-new-thread (fn [] (print "hello\n")))
hello
nil可能出什么事了?
发布于 2014-12-15 23:27:37
这是因为苹果酒/Emacs中的主线程将REPL输出缓冲区绑定到*out*动态变量。这就是为什么你可以在emacs中看到东西。
但是,当您启动一个新线程时,该绑定不存在。修复非常简单--首先创建一个将捕获当前绑定的函数:
(def repl-out *out*)
(defn prn-to-repl [& args]
(binding [*out* repl-out]
(apply prn args)))现在,每当您想要从其他线程打印时,请使用:
(prn-to-repl "hello")就是这样。希望这能有所帮助。
https://stackoverflow.com/questions/27494647
复制相似问题