【问题标题】:Clojure's thread does not show results in Emacs clojure-repl modeClojure 的线程在 Emacs clojure-repl 模式下不显示结果
【发布时间】:2014-12-15 22:55:17
【问题描述】:

我有这个启动并执行函数的 Clojure 代码。

(import [java.lang Thread])
(defn with-new-thread [f]
  (.start (Thread. f)))
(with-new-thread (fn [] (print "hi")))

但是,当我在 emacs 中以 slime-repl 模式运行它时(使用 cider-jack-in 执行,没有打印出任何内容,但返回了 nil。

使用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

可能出了什么问题?

【问题讨论】:

    标签: emacs clojure leiningen


    【解决方案1】:

    这是因为 CIDER/Emacs 中的主线程将 REPL 输出缓冲区绑定到 *out* 动态变量。这就是为什么你可以在 emacs 中看到东西的原因。

    但是,当您启动一个新线程时,该绑定不存在。修复很简单——首先创建一个捕获当前绑定的函数:

    (def  repl-out *out*)
    (defn prn-to-repl [& args]
      (binding [*out* repl-out]
        (apply prn args)))
    

    现在,当您想从不同的线程打印时,请使用:

    (prn-to-repl "hello")
    

    就是这样。希望这可以帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-31
      • 2011-07-11
      • 2014-09-03
      相关资源
      最近更新 更多