python子进程多个stdin.write和stdout.read

标签 python python-3.x pipe subprocess

感谢您花时间回答问题。我正在玩 Python 3.4,我有两个简单的 python 程序。第一,一个名为 test.py 的程序,它接受用户输入并打印一些东西。

while True:
    print("enter something...")
    x = input()
    print(x)
    time.sleep(1)

为了向这个程序发送输入,我有另一个使用子进程的程序:

from subprocess import Popen, PIPE

cat = Popen('python test.py', shell=True, stdin=PIPE, stdout=PIPE)
cat.stdin.write("hello, world!\n")
cat.stdin.flush()
print(cat.stdout.readline())

cat.stdin.write("and another line\n")
cat.stdin.flush()
print(cat.stdout.readline())

但是当我运行上面的程序时,我得到一个错误:

enter something...

hello, world!
Traceback (most recent call last):
  File "/opt/test.py", line 9, in <module>
    x = input()
EOFError: EOF when reading a line
Exception ignored in: <_io.TextIOWrapper name='<stdout>' mode='w' encoding='UTF-8'>
BrokenPipeError: [Errno 32] Broken pipe

如果我将 test.py 替换为标准的 linux 命令(如“cat”),一切都会按预期进行。

有什么方法可以发送多个 stdin 写入并读回多个 stdout?

最佳答案

一般来说,你应该use pexpect for interactive programs (dialog-based interactions) .

您的具体问题可能是由 python 版本不匹配引起的(您认为您的代码是使用 Python 3 执行的,而实际上它可能是使用 Python 2 执行的)。第二个问题 (EOFError) 是预期的:要么在子脚本中捕获它,要么为子脚本提供退出信号(我在下面的代码示例中为此使用了一个空行)。

这是一段在 Python 2 上大声失败的 Python 3 代码:

#!/usr/bin/env python3
import sys
from subprocess import Popen, PIPE

with Popen([sys.executable, '-u', 'test.py'], stdin=PIPE, stdout=PIPE,
           universal_newlines=True, bufsize=1) as cat:
    for input_string in ["hello, world!", "and another line", ""]:
        print(input_string, file=cat.stdin, flush=True)
        print(cat.stdout.readline(), end='')

注意:

这是相应的test.py:

#!/usr/bin/env python3
import time

while True:
    x = input("enter something...")
    if not x: # exit if the input is empty
        break
    print(x)
    time.sleep(1)

输出

enter something...hello, world!
enter something...and another line
enter something...

注意:"enter something..." 之后没有新行

它有效但很脆弱,请阅读 Q: Why not just use a pipe (popen())?use pexpect instead .


如果输入是有限的并且它不依赖于输出那么你可以一次传递它:

#!/usr/bin/env python3
import sys
from subprocess import check_output

output = check_output([sys.executable, 'test.py'],
                      input="\n".join(["hello, world!", "and another line"]),
                      universal_newlines=True)
print(output, end='')

此版本要求子进程正确处理 EOF:

#!/usr/bin/env python3
import time

while True:
    try:
        x = input("enter something...")
    except EOFError:
        break # no more input

    print(x)
    time.sleep(1)

输出是一样的(如上所示)。

关于python子进程多个stdin.write和stdout.read,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33313566/

相关文章:

Python CSV 只写入特定行

Python 3 静态成员

perl - 在 Perl 中重定向管道的 STDOUT

python - 更改父级继承的python类的方法

c - read() write() 通过 dup2() 与 stdin 和 stdout 写入 pipe()

c - C 中使用管道的大写字母

python - 为 docker-compose 文件设置预 Hook

python - 是否可以将日期时间列添加到多对多字段?

python - 有没有一种简单的方法可以将 vulture 与 django 一起使用?

python-3.x - 当处理程序在 AWS lambda 中超时时如何处理剩余的负载?