为什么我不能在 emacs 的 Clojure Cider REPL 中从后台线程打印? [英] Why can't I print from background threads in Clojure Cider REPL in emacs?

查看:15
本文介绍了为什么我不能在 emacs 的 Clojure Cider REPL 中从后台线程打印?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我尝试在我的 emacs cider-repl 中评估以下代码,则按预期返回 nil,但在 repl 缓冲区或控制台中不会进行任何打印.我怎样才能按预期打印出来?

If I try to evaluate the following code in my emacs cider-repl, nil is returned, as expected, but none of the printing takes place in the repl buffer or console. How can I make this print out as intended?

(dotimes [i 5]                                                                                                                                        
  (.start                                                                                                                                             
   (Thread.                                                                                                                                           
    (fn []                                                                                                                                             
      (Thread/sleep (rand 500))                                                                                                                       
      (println (format "Finished %d on %s" i (Thread/currentThread)))))))
;=> nil

这很好,但是:

(println (format "Finished 1 on %s" (Thread/currentThread)))
;=> Finished 1 on Thread[nREPL-worker-18,5,main]
----------- mini-buffer -----------------
nil

推荐答案

println 的行为是使用一个名为 *out* 的动态绑定 var 作为它的输出流.emacs 动态绑定 *out* 以转到 repl 缓冲区以获取在 repl 缓冲区中计算的代码,但是如果您创建一个线程,该线程的 *out* 将获得根绑定*out* 的,在 cider 的情况下不会是 repl 缓冲区.

The behavior of println is to use a dynamically bound var called *out* as its output stream. emacs dynamically binds *out* to go to the repl buffer for code evaluated in the repl buffer, but if you create a thread, that thread's *out* gets the root binding of *out*, which in the case of cider will not be the repl buffer.

如果您使用 cider-jack-in 启动 repl,当您查看缓冲区列表时,应该有一个名称类似于 *nrepl-server* 的缓冲区它包含根 *out* 绑定的输出.这是我运行代码后的内容:

If you started the repl using cider-jack-in, when you look at you buffer list there should be a buffer with a name like *nrepl-server* which contains the output of the root *out* binding. Here is the contents of mine after running your code:

nREPL server started on port 52034 on host 127.0.0.1 - nrepl://127.0.0.1:52034
Finished 1 on Thread[Thread-9,5,main]
Finished 0 on Thread[Thread-8,5,main]
Finished 2 on Thread[Thread-10,5,main]
Finished 3 on Thread[Thread-11,5,main]
Finished 4 on Thread[Thread-12,5,main]

如果您没有使用 cider-jack-in,输出将打印到您启动 nrepl 进程的终端.

If you did not use cider-jack-in, the output will print to the terminal where you started the nrepl process.

这篇关于为什么我不能在 emacs 的 Clojure Cider REPL 中从后台线程打印?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆