【问题标题】:Python subprocess - write multiple stdinPython子进程 - 编写多个标准输入
【发布时间】:2023-10-07 12:29:01
【问题描述】:

我需要打开一个 R 脚本并为其提供由单独的 python 脚本制定的输入。 subprocess 模块似乎是做到这一点的好方法。

虽然我遇到了一些令人费解的结果,即我显然可以通过p.stdin 写一次且只能写一次。这是我到目前为止所拥有的:

from subprocess import Popen, PIPE, STDOUT

p = Popen(['r --no-save'],stdin=PIPE,stdout=PIPE,stderr=PIPE,shell=True)
p.stdin.write("source('myrscript.R')\n")
p.stdin.write('myfirstinput')

当我运行此代码时会发生什么,stdin.write() 的第一个实例按预期执行(并打开我的 R 脚本),但第二行什么也不做,并且子进程(实际上是 R 脚本)以错误,指示子处理在预期输入的地方没有收到输入,因此终止。

注意- 在一个完美的世界中,我会直接通过 R 进行交互,但是这个特定的脚本需要复杂的输入,而不能出于实际目的直接输入。此外,rpy / rpy2 不是一个选项,因为此脚本的最终用户不一定有权访问该模块或其依赖项。 rscript 也不是一个选项(出于多种原因,但主要是因为最终用户 R 配置的可变性)。

最后,p.communicate 不是一个选项,因为显然这会在写入后关闭进程,我需要让它保持打开状态。

提前致谢

【问题讨论】:

  • flush()ing stdin 没有任何改变?
  • 好问题,不,写命令之间包括p.stdin.flush()不会改变结果。
  • 根据 Popen 的子进程模块文档... 注意不要在此函数中使用 stdout=PIPE 或 stderr=PIPE。由于当前进程中未读取管道,因此如果子进程向管道生成足够的输出以填满 OS 管道缓冲区,则子进程可能会阻塞。也许脚本中发生了一些古怪的事情?您是否尝试过仅发出 2 个简单的命令,例如 p.stdin.write("a = 7") p.stdin.write("print(a)") 而不是脚本?
  • 这是一个有用的建议,谢谢。它确实似乎是我的脚本内部的东西,因为当我发出 2 个单独的命令时,正如你所建议的,我从 p.stdin.write() 得到了预期的行为。我将对 R 脚本进行故障排除 - 非常感谢!
  • 嗯,好的。您有权访问此脚本吗?至少读取标准输入的相关部分?要查看为什么我要求检查帮助(stdin)末尾的注释

标签: python r subprocess


【解决方案1】:

您需要拨打.communicate():

from subprocess import Popen, PIPE, STDOUT

p = Popen(
    ['r', '--nosave'],
    stdin=PIPE,
    stdout=PIPE,
    stderr=PIPE)
p.stdin.write("source('myrscript.R')\n")
p.stdin.write('myfirstinput\n')
p.stdin.write('q\n')

stdout, stderr = p.communicate()

print '---STDOUT---'
print stdout
print '---STDERR---'
print stderr
print '---'

讨论

  • 我不使用shell=True,它似乎与我的 fake R 脚本一起工作,因为我的系统中没有安装 R。您可能需要也可能不需要。
  • 我更喜欢将命令行分解成一个字符串列表,如图所示,但是像r --nosave 这样的单个字符串也可以;只是不要同时做这两件事。
  • 不要忘记stdin.write() 不会写入换行符\n,您必须自己提供。

更新

我的第一次尝试不成功,我希望第二次尝试更接近。正如 J.F. Sebastian 建议的那样,您可能想使用pexpect

import pexpect
import sys

if __name__ == '__main__':
    prompt = '> ' # Don't know what the R prompt looks like
    lines = ['one', 'two', 'three']

    r = pexpect.spawn('r --no-save', logfile=sys.stdout)
    for line in lines:
        r.expect(prompt)
        r.sendline(line)

    # If you want to interact with your script, use these two lines
    # Otherwise, comment them out
    r.logfile = None # Turn off logging to sys.stdout
    r.interact()

讨论

  • 您可能需要安装pexpect。我用pip install pexpect做到了
  • 如果您不想与系统交互,请注释掉最后两行,但请确保发送一些信号让 R 脚本退出。
  • spawn() 返回一个 spawn 对象,请参阅文档 here

【讨论】:

  • OP 已经说过 p.communicate() 不合适,因为它等待进程完成。还要使用p.communicate("\n".join(["source('myrscript.R')", "myfirstinput", "q"])) 而不是p.stdin.write() 调用,否则可能会导致死锁