SBCL:在运行时收集运行程序进程的输出 [英] SBCL: Gather output of run-program process while running
问题描述
尊敬的StackExchange成员,
Dear StackExchange members,
我最近开始玩弄Common Lisp并想创建一个用于管理经过修改的Minecraft服务器的Web界面。我已经尝试过此解决方案,但是在这种情况下,该功能只是挂起而从不返回。
I recently began toying around with Common Lisp and want to create a web interface for administrating a modded Minecraft server. I already tried out this solution but in this case the function just hangs and never returns.
我的代码如下:
(defvar *proc*)
(defun create-minecraft-proc ()
(let* ((binary "/usr/bin/java")
(jar "/home/user/temp/Vanilla 1.8/minecraft.jar")
(dir "/home/user/temp/Vanilla 1.8")
(args (list "-jar" jar "nogui")))
(setf *proc* (sb-ext:run-program binary args :directory dir :wait nil :input :stream :output :stream))))
(defun copy-stream (in out)
(loop for line = (read-line in nil nil)
while line
do (write-line line out)))
(defun get-minecraft-output ()
(with-open-stream (proc-stream (process-output *proc*))
(with-output-to-string (out)
(copy-stream (process-output *proc*) out)))
如何
推荐答案
使用 / bin / cat
,我发现了一些可能对您有所帮助的东西。
Experimenting with /bin/cat
, I found some things that might help you.
首先,始终使用完成输出
,写入进程的输入后:
First, of all, always use finish-output
after writing to the process's input:
(format (process-input *cat*) "Hello~%")
(finish-output (process-input *cat*))
否则,输入可能直到关闭输入流才到达子流程。如果Minecraft在生成任何输出之前需要输入,那么
如果尝试在没有所需输入的情况下从其输出中读取,将导致SBCL挂起,而这种方式无法用SLIME中的Cc Cc恢复。
Otherwise, the input may not reach the subprocess until you close the input stream. If Minecraft requires input before it'll generate any output, then trying to read from its output without the required input would result in SBCL hanging in a way that can't be recovered with C-c C-c in SLIME.
第二,使用监听
确定是否已生成任何输出:
Second, use listen
to determine if any output has been generated:
(when (listen (process-output *cat*))
(read-line (process-output *cat*)))
具有完成产出
和收听
,在逐渐从cat的标准输出中读取时,我能够避免挂起。
With finish-output
and listen
, I was able to avoid hanging while incrementally reading from cat's stdout.
这篇关于SBCL:在运行时收集运行程序进程的输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!