为什么在使用 lein run 按下 ENTER(似乎挂起)后 read-line 没有返回,但可以与 lein repl 一起使用? [英] Why does read-line not return after hitting ENTER (seems like a hang) using lein run, but works with lein repl?
问题描述
手头的问题是,当我用 lein run
运行我的程序时,它进入了 (read-line)
部分,我无法摆脱它,意思是:读行永远不会返回.
The problem at hand is that when I run my program with lein run
it gets to the (read-line)
part and I can't get out of it, meaning: read-line never returns.
相关代码如下:
(def command (atom ""))
(defn print-prompt []
(print "prompt> ")
(flush)
)
(defn ask-for-input []
(print-prompt)
(let [x (str (read-line))]
(println (str "User input: " x))
(reset! command x)
)
)
我从未在屏幕上看到用户输入:"字符串.奇怪的是,如果我运行 lein repl
并调用 (ask-for-input)
然后它可以正常工作:S
I never see the "User input: " string on screen.
The strange part is, if I run lein repl
and call (ask-for-input)
then it works correctly :S
推荐答案
试试 lein trampoline run,它有效.
以下内容来自 leiningen 常见问题解答:
The following is from leiningen FAQ:
问:我无法访问我的项目中的 stdin.
Q: I don't have access to stdin inside my project.
A:这是JVM的进程处理方法的限制;他们都没有正确公开标准输入.这意味着像 read-line 这样的函数在大多数情况下不会按预期工作,尽管 repl 任务必须包含一个解决方法.您还可以使用 trampoline 任务在 Leiningen 退出后启动项目的 JVM,而不是将其作为子进程启动.
A: This is a limitation of the JVM's process-handling methods; none of them expose stdin correctly. This means that functions like read-line will not work as expected in most contexts, though the repl task necessarily includes a workaround. You can also use the trampoline task to launch your project's JVM after Leiningen's has exited rather than launching it as a subprocess.
这篇关于为什么在使用 lein run 按下 ENTER(似乎挂起)后 read-line 没有返回,但可以与 lein repl 一起使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!