繁体   English   中英

Emacs中的Python子进程未获取输入或发送回结果

[英]Python Sub-Process in Emacs Not Getting Input or Sending Back Results

背景

我正在编写一个小程序包,将Emacs Lisp计算分配到Python子进程中,因此不必重现复杂的数值例程。 对于我的特定问题,我需要将Python作为子进程,因为我需要维护由Python完成的某些耗时的计算。 对于小事情,我可以通过外壳命令将计算发送到Python来做得很好。 但是,我确实需要该子流程来处理更深入的内容。

设定

  1. Windows 10下的GNU Emacs 25.2.1

  2. Anaconda的Windows 10的Python 3.5发行版

样品运行

从任何缓冲区中,击中Mx 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>

在IELM中运行上述Emacs Lisp时(或您想要的任何方式),您可以观察python缓冲区。 什么也没显示。 但是,正如您清楚看到的那样,我可以终止该进程并查询其运行状态。

问题

为什么Python进程无法在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))

因为我无法使用功能start-process来启动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”。 如果您想很好地查看此工作,只需使用Cx 3垂直吐出窗口即可。在右侧切换到Python ,然后从左侧缓冲区运行代码。

当您准备终止Python进程时,

(process-send-eof PyProc)

杀死缓冲区以进行清理。 希望这对尝试控制Windows平台上的进程的人员有所帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM