【发布时间】:2016-08-23 14:36:04
【问题描述】:
我想在 Clojure 中维护一个长时间运行的交互式 shell 进程——这在 Emacs 中被称为“劣等”进程。我认为基本思想很熟悉,因为 Clojure 本身可以通过 CIDER 在 Emacs 内部运行。我很高兴知道是否有任何使用 Clojure 作为顶级进程的设置的工作示例。
编辑:我发现了这个“shell”Gist,它是Conch 的一个非常漂亮的包装器,让我深思。
使用我自己的first attempt with Conch:我无法将字符串输入python 并以与cat 相同的方式返回输出。但是通过一些实验,我弄清楚了基础知识并绕过了第一个障碍。
(require '[me.raynes.conch.low-level :as sh])
(def sh-python (sh/proc "python" "-i")) ; "-i" needed to get interactive mode
#'flowrweb.core/sh-python
(future (sh/stream-to-out sh-python :out))
#future[{:status :pending, :val nil} 0x516f3fff]
(sh/feed-from-string sh-python "1+1\n") ; just returns nil on the CIDER repl
2 ; <- but we see "2" with the lein repl
nil
所以我知道我感兴趣的数据是可用的,虽然它在 CIDER 中不打印有点奇怪(子问题...为什么不是 2打印?)。无论如何,对于我的用例,我不需要它打印;相反,我只想将它作为字符串取回。
第一次尝试:
(def pyout (future (sh/stream-to-string sh-python :out)))
;=> #'flowrweb.core/py-out
(sh/feed-from-string sh-python "1+2\n")
;=> 3
;=> nil
@pyout
^C ; <- the process hangs
看来sh/stream-to-string 并没有完全满足我的需要。
用with-out-str 怎么样?
(def something (with-out-str (sh/feed-from-string sh-python "1+3\n")))
;=> 4
;=> #'user/something
something
;=> "" ;<- where is the "4"?
不,那也没用。
tl;dr:如何重定向来自 Conch 子流程的输出以供将来处理?
【问题讨论】:
-
现在我已经阅读了更多关于期货的内容,我可以看到“第一次尝试”有点愚蠢。未来不会完成,这就是进程挂起的原因。不确定
with-out-str尝试有什么问题!