【问题标题】:Python interactive shell not responding to input when run in subprocessPython交互式shell在子进程中运行时不响应输入
【发布时间】:2021-06-20 03:43:09
【问题描述】:

我正在制作一个终端命令行界面程序,作为一个更大项目的一部分。我希望用户能够运行任意命令(如在 cmd 中)。问题是当我使用subprocess 启动python 进程时,python 不会向stdout 写入任何内容。我什至不确定它是否读到了我在stdin 中写的内容。这是我的代码:

from os import pipe, read, write
from subprocess import Popen
from time import sleep

# Create the stdin/stdout pipes
out_read_pipe_fd, out_write_pipe_fd = pipe()
in_read_pipe_fd, in_write_pipe_fd = pipe()

# Start the process
proc = Popen("python", stdin=in_read_pipe_fd, stdout=out_write_pipe_fd,
             close_fds=True, shell=True)

# Make sure the process started
sleep(2)

# Write stuff to stdin
write(in_write_pipe_fd, b"print(\"hello world\")\n")

# Read all of the data written to stdout 1 byte at a time
print("Reading:")
while True:
    print(repr(read(out_read_pipe_fd, 1)))

当我将"python" 更改为"myexe.exe" 时,上面的代码有效,其中myexe.exe 是我用MinGW 编译的C++ 编写的hello world 程序。为什么会这样? This 是完整代码,但上面的示例显示了我的问题。当我将"python" 更改为"cmd" 时,它也可以正常工作。

PS:当我在命令提示符下运行 python 时,它给了我:

Python 3.7.9 (tags/v3.7.9:13c94747c7, Aug 17 2020, 18:58:18) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>

这意味着应该有东西写到stdout

【问题讨论】:

  • 你确定 REPL 输出是标准输出吗?
  • @OneCricketeer 好吧,我假设 python 进程应该写入标准输出,因为当我从 cmd 运行它时,它会将内容写入屏幕。是这个意思吗?
  • @OneCricketeer 我检查了 stderr 它也是空的
  • 所以,你永远不会为 REPL 发送“输入键”/换行符来实际执行任何操作,那么为什么不直接使用 eval() 而不是子shell?
  • 另一方面,将 exit()\n 发送到标准输入 - 而不是 hello world - 并没有关闭执行,所以这可能也不起作用。

标签: python python-3.x subprocess pipe


【解决方案1】:

问题

请注意,您将 python 连接到非 tty 标准输入,因此它的行为与您在终端中运行命令 python 时不同。相反,它的行为就像您使用了命令cat script | python,这意味着它会等到标准输入关闭,然后将所有内容作为单个脚本执行。 docs 中描述了此行为:

解释器的操作有点像 Unix shell:当被调用时 标准输入连接到 tty 设备,它读取并执行 交互式命令;当使用文件名参数或使用 一个文件作为标准输入,它从中读取并执行一个脚本 文件。

在阅读前尝试添加close(in_write_pipe_fd),你会发现它成功了。

方案一:强制python交互运行

为了解决您的问题,我们需要 python 来忽略它不是交互式运行的事实。运行 python --help 时,您可能会注意到标志 -i

-i     : inspect interactively after running script; forces a prompt even
         if stdin does not appear to be a terminal; also PYTHONINSPECT=x

听起来不错 :) 只需将您的 Popen 调用更改为:

Popen("python -i", stdin=in_read_pipe_fd, stdout=out_write_pipe_fd,
      close_fds=True, shell=True)

一切都应该开始按预期工作了。

方案二:假装终端

您可能听说过pty,一种伪终端设备。这是一些操作系统中的一个功能,它允许您将管道连接到 tty 驱动程序而不是终端仿真器,因此,在您的情况下,允许您自己编写终端仿真器。您可以使用 python 模块pty 打开一个并将其连接到子进程而不是普通管道。这会让 python 认为它连接到一个实际的 tty 设备,并且还允许你模拟 Ctrl-C 按下、向上/向下箭头等。

但这是有代价的——一些程序在连接到 tty 时,也会相应地改变它们的输出。例如,在许多 linux 发行版中,grep 命令为输出中的匹配模式着色。如果您不确定您可以在程序中正确处理颜色,或者配置 tty 以声明它不支持颜色(和其他 tty 功能),您将开始在某些命令的输出中得到垃圾。

小记

我确实觉得这可能不是实现目标的最佳方法。如果您更详细地描述它,我也许可以帮助您考虑替代方案:)

【讨论】:

  • 我确认这是问题所在。添加close(in_write_pipe_fd) 有效。有没有办法制作stdin tty?管道是 tty 是什么意思?
  • 太棒了。为什么在运行 python 时不直接使用-i 标志?
  • 在我的终端中,用户可以运行任意命令,所以我的终端基本上是 tkinter 中的 cmd。如果我不必强制用户使用-i 标志会更好。我仍然认为这是我一直在寻找的答案,并且在 1 小时内我会给你 +100 声望。
  • 您只需要使用该标志运行子进程,如下所示:Popen("python -i", stdin=in_read_pipe_fd, stdout=out_write_pipe_fd, close_fds=True, shell=True)
  • 在我的终端中运行"cmd",用户可以与之交互。当我露营/运行我的 C++ 文件时它可以工作,但是当我运行 python 时它没有在我的屏幕上放置任何东西。我只会强制用户在调用 python 时使用-i 标志。
【解决方案2】:

python 解释器更常用于从命令行运行脚本而不是交互模式,因此它的交互元素不会写入stdout,否则它们会干扰脚本输出。没有人愿意从脚本输出中删除介绍性文本。

为了促进这一点,在与用户交互时,解释器使用sys.displayhook 方法故意将输出发送到stdout,否则不会发送到stdout。其余部分(例如介绍文本和>>> 提示)根据the docs 写入stderr

  • stdin 用于所有交互式输入(包括对 input() 的调用);
  • stdout 用于 print() 和表达式语句的输出以及 input() 的提示;
  • 解释器自己的提示及其错误消息转到 stderr。

【讨论】:

  • 这看起来像是一个合理的解释,但为什么 python 不响应 print("Hello world")\n 被写入 stdin 呢? stderr 也是空的。
  • 我想知道交互式界面是否使用与底层解释器不同的流? subprocess.run("python", input=b"print('hello')") 按预期工作。
  • 这是可能的,但这并不意味着我不能从cmd 运行python。我认为cmd 只收听stdout/stderr 所以如果python 使用它自己的流cmd 不应该在屏幕上显示任何东西。 IDLE 也捕获进程stdoutstderr。我会尝试阅读 IDLE 的源代码。
猜你喜欢
  • 2017-09-19
  • 2013-03-28
  • 1970-01-01
  • 1970-01-01
  • 2023-03-09
  • 2013-11-21
  • 2011-09-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多