Python的子过程没有得到输入或发回结果
我写一个小包装农场出来的Emacs Lisp计算为Python子进程,所以我没有重现复杂数字例程。对于我的特殊问题,我需要Python作为子进程,因为我需要维护Python完成的一些耗时的计算。对于小事,我可以通过shell命令将计算发送到Python来完成很好的事情。但是,我需要更密集的东西的子过程。
SETUP
GNU Emacs的25.2.1的Windows 10下
蟒蛇的的Python 3.5分发用于Windows 10
样品运行
从任何缓冲区中,点击M-x ielm。这将启动IELM Emacs Lisp外壳。这里是我的会话中的几分钟前:
*** Welcome to IELM *** Type (describe-mode) for help.
ELISP> (setq PyProc (start-process "python" "*python*" "python"))
#<process python>
ELISP> (process-list)
(#<process python> #<process ielm>)
ELISP> (process-status PyProc)
run
ELISP> (process-type PyProc)
real
ELISP> (process-send-string PyProc "1+1")
nil
ELISP> (process-send-eof PyProc)
#<process python>
当你运行上面的Emacs Lisp在IELM(或任何你想要的方式),你可以观看蟒蛇缓冲区。没有显示。然而,正如你清楚地看到的,我可以杀死进程并查询它的运行状态。
问题
为什么Python进程无法显示在蟒蛇缓冲什么吗?我已经使用MySQL来完成远程服务器(使用本地安装的MySQL)而没有任何问题。
我已经做了一些调查。我有一个部分的解决方法。但是,我仍然不知道为什么使用启动过程的原始尝试不起作用。
我知道,一个可以使用
M-x run-python
推出劣质Python进程。然后我们可以使用使用
(process-send-string PyProc "1+1\n")
其中PyProc是牵着你的进程对象的符号。这将命令发送给劣质进程并按回车键。奇怪的是,“1 + 1 \ n”不显示在Python缓冲区中。返回值显示。在输出之前和之后存储点,可以轻松地检索Python的输出。以下是部分摘录:
;; Run inferior Python process (assumes you have Python properly installed)
(setq PyProc (run-python))
;; Send a carriage return. There is some issue when the process first starts
(process-send-string PyProc "\n")
;; Store the point prior to sending a command
(setq PyPM (with-current-buffer "*Python*"
(point)
)
)
;; Send a command to Python
(process-send-string PyProc "1+1;\n")
;; Get the result of the computation
(setq Result (with-current-buffer "*Python*"
(buffer-substring-no-properties PyPM
(- (point) 5)
)
)
)
效果评价给你
Result -> "2"
可以使用
(process-send-eof PyProc)
尽管如此终止进程不知道为什么我不能简单地用启动过程。
在我的第一个回答上面,我使出
(setq PyProc (run-python))
运行Python作为一个子进程,因为我一直没能使用的功能的启动进程来运行Python。根据GNU Emacs文档,DOS程序可能存在很多问题,因为它们处理stdin和stdout的方式。但是,python提供了一个“-i”命令行标志,可以解决Windows上的问题。
所有你需要做的是:
(setq PyProc (start-process "python" "*Python*" "python" "-i"))
(process-send-string PyProc "1+1;\n")
如果切换到缓冲的Python,你会看到“2”作为计算的结果。如果你想很好地看到这个工作,只需使用C-x 3垂直吐出你的窗口。切换到Python在右边并从左边的缓冲区运行你的代码。
当您准备杀死Python进程,只是
(process-send-eof PyProc)
杀了你缓存清理。希望这有助于那些试图控制Windows平台上的进程。