为什么在使用lein run按下ENTER键(好像是挂起)后,读行不返回,但与lein repl一起工作? [英] Why does read-line not return after hitting ENTER (seems like a hang) using lein run, but works with lein repl?

查看:91
本文介绍了为什么在使用lein run按下ENTER键(好像是挂起)后,读行不返回,但与lein repl一起工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

眼前的问题是,当我使用lein run运行程序时,它进入了(read-line)部分,而我走不开了,这意味着:读取行永不返回.

以下是相关代码:

(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

解决方案

尝试蹦床跑步,它可以工作.

以下摘自leiningen常见问题解答:

问:我无法在项目中访问stdin.

A:这是对JVM进程处理方法的限制;他们都没有正确地暴露标准输入.这意味着,尽管repl任务必须包含一种解决方法,但像read-line这样的功能在大多数情况下都无法正常工作.您也可以使用蹦床任务在Leiningen退出后启动项目的JVM,而不是将其作为子进程来启动.

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.

Here is the relevant code:

(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)
    )
)

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

解决方案

Try lein trampoline run, it works.

The following is from leiningen FAQ:

Q: I don't have access to stdin inside my project.

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键(好像是挂起)后,读行不返回,但与lein repl一起工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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