在 Clojure 中,如何向 Clojure.java.shell/sh 创建的进程发送退出命令

In Clojure, how do I send an exit command to a process created by Clojure.java.shell/sh

我正在尝试 运行 来自 clojure 的 pocketsphinx。我写了下面的 .sh 脚本

unbuffer pocketsphinx_continuous -innmic yes > pipe 并且我想使用 (shell/sh).

调用此过程

Unbuffer 允许 pocketsphinx 将其数据正确地刷新到管道中,以便可以逐行读取数据。

我遇到的问题是我不知道如何正确终止进程。它将永远 运行 而永远不会 return 控制。如果我终止线程 运行ning,sh 命令生成的进程仍然 运行s。我唯一能想到的就是 运行ning kill on the pid

clojure.java.shell/sh 不会 return 直到正在执行的进程完成并且您需要使用另一个线程来对 pid 使用 kill。

另一个解决方案是使用 java.lang.Runtime.exec or java.lang.ProcessBuilderto get a java.lang.Process instance which provides methods like destroy or destroyForcibly.

您也可以使用 Raynes/conch library if you still would like to have a nice Clojure API and still be able to run a command and specify a timeout for the process to complete and still get the output produces until the timeout occured