如何在 python 上多次向另一个程序提供输入

How to give input multiple times to another program on python

我想制作 python 文件来打开两个程序。这两个程序必须多次相互获取输入。我打开了两个程序,知道如何给一个程序输入,但我不知道如何在一个程序上多次输入并多次输出。我的代码是这样的。

subprocess.call("/usr/bin/gcc -o p1 "+path1,shell=True)
subprocess.call("/usr/bin/gcc -o p2 "+path2,shell=True)
cmd_1 = subprocess.Popen("./p1",shell = True,stdin = subprocess.PIPE,stdout = subprocess.PIPE,stderr = subprocess.PIPE)
cmd_2 = subprocess.Popen("./p2",shell = True,stdin = subprocess.PIPE,stdout = subprocess.PIPE,stderr = subprocess.PIPE)
std_out_1 = cmd_1.stdout
std_out_2 = cmd_2.stdout
for line in std_out_1.readlines():
    print(line.decode('ascii'))
for line in std_out_2.readlines():
    print(line.decode('ascii'))

现在这个程序只是获取程序输出。我想为每个程序提供 N 次输入并获得 N 次输出。所以我希望我的代码是这样的。

give_input(n)
for i in range(n):
    t_1 = get_output(t_2) //give input t_2, and get output t_1
    t_2 = get_output(t_1) //give input t_1, and get output t_2

尝试使用 writecommunicate 将数据发送到 cmd_1cmd_2 并获得响应,请参阅 https://docs.python.org/3/library/subprocess.html#popen-constructor

for i in range(n):
    cmd_1 = subprocess.Popen("./p1",shell = True,stdin = subprocess.PIPE,stdout = subprocess.PIPE,stderr = subprocess.PIPE)
    cmd_2 = subprocess.Popen("./p2",shell = True,stdin = subprocess.PIPE,stdout = subprocess.PIPE,stderr = subprocess.PIPE)
    cmd_1.stdin.write(cmd_input[n])
    cmd_2.stdin.write(cmd_input[n])
    cmd1_stdout, cmd1_stderr = cmd_1.communicate()
    cmd2_stdout, cmd2_stderr = cmd_2.communicate()
    for line in cmd1_stdout:
        print(line.decode('ascii'))
    for line in cmd2_stdout:
        print(line.decode('ascii'))

通过管道动态交换消息(未测试):

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

with Popen('p1', stdin=PIPE, stdout=PIPE, universal_newlines=True) as p1, \
     Popen('p2', stdin=PIPE, stdout=PIPE, universal_newlines=True) as p2:
    # give_input(n)
    print(n, file=p1.stdin, flush=True)
    print(n, file=p2.stdin, flush=True)
    for i in range(n):
        # t_1 = get_output(t_2) //give input t_2, and get output t_1
        print(p1.stdout.read(1), file=p2.stdin, flush=True)
        # t_2 = get_output(t_1) //give input t_1, and get output t_2
        print(p2.stdout.read(1), file=p1.stdin, flush=True)

它假设子进程期望一行作为请求,return一个字符作为响应。

让它发挥作用:

  1. p1p2 应禁用其内部 stdout 缓冲(在程序开始时调用 setvbuf(stdout, NULL, _IONBF, 0); 或在每个 printf()。相关:Python C program subprocess hangs at “for line in iter”
  2. 将带有 p1p2 的目录放入 $PATH 或将完整路径 'p1' 放入 Popen().