往返于2个Python子进程的循环管道

往返于2个Python子进程的循环管道

本文介绍了往返于2个Python子进程的循环管道的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要有关子流程模块的帮助.这个问题听起来似乎很重复,而且我已经以多种方式看到了许多与之相关的文章.但是即使这样我也无法解决我的问题.内容如下:
我有一个C程序2.c,其内容如下:

I needed help regarding the subprocess module. This question might sound repeated, and I have seen a number of articles related to it in a number of ways. But even so I am unable to solve my problem. It goes as follows:
I have a C program 2.c it's contents are as follows:

#include<stdio.h>
int main()
{
int a;
scanf("%d",&a);
while(1)
  {
   if(a==0)               //Specific case for the first input
     {
      printf("%d\n",(a+1));
      break;
     }
   scanf("%d",&a);
   printf("%d\n",a);
  }
return 0;
}

我需要编写一个python脚本,该脚本首先使用subprocess.call()编译代码,然后使用Popen打开两个进程以执行相应的C程序.现在,第一个过程的输出必须是第二个过程的输入,反之亦然.因此,从本质上讲,如果我的初始输入为0,则第一个进程输出为2,第二个进程取该输出.反过来,它会无限次输出3,依此类推.

I need to write a python script which first compiles the code using subprocess.call() and then opens two process using Popen to execute the respective C-program. Now the output of the first process must be the input of the second and vice versa. So essentially, if my initial input was 0, then the first process outputs 2, which is taken by second process. It in turn outputs 3 and so on infinitely.

下面的脚本是我的初衷,但这是有缺陷的.如果有人可以帮助我,我将非常感谢.

The below script is what I had in mind, but it is flawed. If someone can help me I would very much appreciate it.

from subprocess import *
call(["gcc","2.c"])
a = Popen(["./a.out"],stdin=PIPE,stdout=PIPE) #Initiating Process
a.stdin.write('0')
temp = a.communicate()[0]
print temp
b = Popen(["./a.out"],stdin=PIPE,stdout=PIPE) #The 2 processes in question
c = Popen(["./a.out"],stdin=PIPE,stdout=PIPE)
while True:
    b.stdin.write(str(temp))
    temp = b.communicate()[0]
    print temp
    c.stdin.write(str(temp))
    temp = c.communicate()[0]
    print temp
a.wait()
b.wait()
c.wait()

推荐答案

问题在这里

while True:
    b.stdin.write(str(temp))
    temp = b.communicate()[0]
    print temp
    c.stdin.write(str(temp))
    temp = c.communicate()[0]
    print temp

一旦 communicate 返回,它不会引起更多注意.您必须再次运行该过程.另外,您不需要同时打开两个进程.

Once communicate has returned, it does noting more. You have to run the process again. Plus you don't need 2 processes open at the same time.

此外,init阶段与运行阶段没有什么不同,不同之处在于您提供了输入数据.

Plus the init phase is not different from the running phase, except that you provide the input data.

您可以做些什么来简化并使其工作:

what you could do to simplify and make it work:

from subprocess import *
call(["gcc","2.c"])
temp = str(0)

while True:
    b = Popen(["./a.out"],stdin=PIPE,stdout=PIPE) #The 2 processes in question
    b.stdin.write(temp)
    temp = b.communicate()[0]
    print temp
    b.wait()

否则,要查看2个进程并行运行,证明可以做到这一点,只需按以下步骤修复循环(通过在循环中移动 Popen 调用)即可:

Else, to see 2 processes running in parallel, proving that you can do that, just fix your loop as follows (by moving the Popen calls in the loop):

while True:

    b = Popen(["./a.out"],stdin=PIPE,stdout=PIPE) #The 2 processes in question
    c = Popen(["./a.out"],stdin=PIPE,stdout=PIPE)

    b.stdin.write(str(temp))
    temp = b.communicate()[0]
    print temp
    c.stdin.write(str(temp))
    temp = c.communicate()[0]
    print temp

更好.b输出提供c输入:

better yet. b output feeds c input:

while True:

    b = Popen(["./a.out"],stdin=PIPE,stdout=PIPE) #The 2 processes in question
    c = Popen(["./a.out"],stdin=b.stdout,stdout=PIPE)

    b.stdin.write(str(temp))
    temp = c.communicate()[0]
    print temp

这篇关于往返于2个Python子进程的循环管道的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 03:15