Python子进程 - 写多个stdin

时间:2013-04-03 14:47:19

标签: python r subprocess

我需要打开一个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脚本)退出带有错误,表示子进程没有收到预期输入的输入,因此终止。

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

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

提前致谢

1 个答案:

答案 0 :(得分:4)

您需要拨打.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,因为我的系统中没有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
相关问题